如何添加case语句以在每条记录后添加break?

时间:2017-06-03 05:24:24

标签: php mysql while-loop line-breaks sql-like

找到LIKE 'A%'的下一个记录后,每行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

更新的代码在这里

$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

3 个答案:

答案 0 :(得分:1)

由于您按row_name排序,因此只需检查新值是否与之前的值不同。

//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>";
       }

答案 1 :(得分:0)

您可以将此功能用于php中的like

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

实施例

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

答案 2 :(得分:0)

这应该在每个唯一行之间(而不是在第一行之前)添加换行符,并将row_name字母添加到每个唯一行的开头。

$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>";
}

为了克服OP的li {float:left}造型,他在css中做了这个调整:

ul{display: table-cell; }

旧答案从这里开始:

我不知道Bijender的回答是如何帮助你的,我也不知道为什么你需要一个函数调用。我也不知道你的意思&#34;在LIKE&#39; A%&#34;之后添加休息。

由于row_name是单个字母值,并且您只想返回值为A,B,C,D,E,F,G,H,I或J的行,因此您需要这个更简单/更精确的查询:

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

我在帖子中发现的错误是您的表格列为EventIdEvent_Id。为了清晰起见,你应该修正这个错误。