Search code examples
phpmysqlwhile-loopline-breakssql-like

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


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.

enter image description here

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**

enter image description here


Solution

  • 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.