How to add case statement to add break after every record?

旧时模样 提交于 2020-01-02 10:07:24

问题


There should be a break after every row LIKE 'A%' when find next records of LIKE 'B%'.

$stmt = $con->prepare("SELECT * FROM fistevent WHERE Event_Id=? AND TicketType=? AND row_name REGEXP '^[A-Z]' ORDER BY row_name ASC");

$stmt->bind_param("ss", $_POST['EventId'],$_POST['TicketType']);
$stmt->execute();
$result = $stmt->get_result();
$numRows = $result->num_rows;
if($numRows > 0) {
    echo ' <div class="register">';
    while($r = $result->fetch_assoc()) {
        $EvntId = $r['Event_Id'];
        $RowName = $r['row_name'];

        echo '<ul id="sub" class="sub">';
            if($r['seats'] == null){ 
                echo '<li class="BlankSeat" ></li>';
            }elseif($r['Status'] == 'Y' || $r['Status'] == 'Hold'){
                echo '<li class="occupied" title="Row'.$r["seats"].'" name="'.$RowName.'" value="'.$r["seats"].'"></li>'; 
            }else{
                echo '<li class="Available" title="Row'.$r["seats"].'" name="'.$RowName.'" value="'.$r["seats"].'"></li>';
            }
        echo "</ul>";
   }
   echo '</div>';

I am getting the following output, but I want after `LIKE 'A%'` the next `LIKE 'B%'` should be in the next row.

Updated code is here

$stmt = $con->prepare("SELECT * FROM fistevent WHERE Event_Id=? AND TicketType=? AND row_name REGEXP '^[A-Z]' ORDER BY row_name ASC");
$stmt->bind_param("ss", $_POST['EventId'],$_POST['TicketType']);
$stmt->execute();
$result = $stmt->get_result();
$numRows = $result->num_rows;
if($numRows > 0) {
    echo ' <div class="register">';
    $prev='A';  
    while($r = $result->fetch_assoc()) {
        $EvntId = $r['Event_Id'];
        $RowName = $r['row_name'];
        if($prev!=$RowName){
            echo "<br>";
            $prev=$RowName;
        }
        echo '<ul id="sub" >';
            if($r['seats'] == null){ 
                echo '<li class="BlankSeat" ></li>';
            }elseif($r['Status'] == 'Y' || $r['Status'] == 'Hold'){
                echo '<li class="occupied" title="Row'.$r["seats"].'" name="'.$RowName.'" value="'.$r["seats"].'"></li>'; 
            }else{
                echo '<li class="Available" title="Row'.$r["seats"].'" name="'.$RowName.'" value="'.$r["seats"].'"></li>';
            }
        echo "</ul>";
    }
    echo '</div>';
}else{
    echo "No seats Available";
}

**in responsive**


回答1:


This should add line breaks between (not before the first) each unique row, and add the row_name letter to the start of each unique row.

$prev='';
while($r=$result->fetch_assoc()) {
    if($prev!=$r['row_name']){
        if($prev!=''){echo "<br>";}                             // add breaks between rows
        echo "<ul id=\"sub\" class=\"sub\">{$r['row_name']} ";            // label the row
        $prev=$r['row_name'];                                 // store new row_name letter
    }else{
        echo "<ul id=\"sub\" class=\"sub\">";   // not a new row_name, extend existing row
    }
        if($r['seats']==null){ 
            echo "<li class=\"BlankSeat\"></li>";
        }elseif($r['Status']=='Y' || $r['Status']=='Hold'){
            echo "<li class=\"occupied\" title=\"Row{$r['seats']}\" name=\"{$r['row_name']}\" value=\"{$r["seats"]}\"></li>"; 
        }else{
            echo "<li class=\"Available\" title=\"Row{$r['seats']}\" name=\"{$r['row_name']}\" value=\"{$r['seats']}\"></li>";
        }
    echo "</ul>";
}

To overcome the OP's li {float:left} styling, he has made this adjustment in the css:

ul{display: table-cell; }


Old answer starts here:

I don't know how Bijender's answer helped you, and I don't know why you need a function call. I also don't know what you mean by "add break after LIKE 'A%".

Since row_name is a single letter value, and you want to only return rows with the value A, B, C, D, E, F, G, H, I, or J, then you will want this simpler / more refined query:

SELECT *
FROM fistevent
WHERE Event_Id=? AND TicketType=? AND row_name IN ('A','B','C','D','E','F','G','H','I','J')
ORDER BY row_name ASC

The error that I found in your post is that your table column is either EventId or Event_Id. You should fix this typo for clarity.




回答2:


Since you order by row_name you only need to check if the new value is different from the previous value.

//setting firstLetter
$firstLetter = '1';
  while($r = $result->fetch_assoc()) {
        $EvntId = $r['Event_Id'];
        $RowName = $r['row_name'];


       echo '<ul id="sub" class="sub">';
        if($r['seats'] == null)
        { 
        echo '<li class="BlankSeat" ></li>';
        }
        else if($r['Status'] == 'Y' || $r['Status'] == 'Hold')
        {
        echo '<li class="occupied" title="Row'.$r["seats"].'" name="'.$RowName.'" value="'.$r["seats"].'"></li>'; 
        }
        else
        {
         echo '<li class="Available" title="Row'.$r["seats"].'" name="'.$RowName.'" value="'.$r["seats"].'"></li>';
        }
        //comparing current first letter with previous first letter,also skipping the break if it is the first row
         if($RowName[0] != $firstLetter &&  $firstLetter !='1'  ){
         echo "<br />";
        }
        $firstLetter = $RowName[0];

       echo "</ul>";
       }



回答3:


You can use this function for like in php

function like_match($pattern, $subject)
{
    $pattern = str_replace('%', '.*', preg_quote($pattern, '/'));
    return (bool) preg_match("/^{$pattern}$/i", $subject);
}

Example

like_match('%A','Apple'); //TRUE
like_match('%c', 'coop'); //TRUE
like_match('%l', 'deke'); //FALSE
like_match('%m', 'flip-flop'); //FALSE


来源:https://stackoverflow.com/questions/44340615/how-to-add-case-statement-to-add-break-after-every-record

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!