在查询

时间:2017-05-01 21:05:51

标签: php mysql

我有一个查询,我在一个开关案例中设置。 while循环遍历我的行,并打印出3个randoms行,其中打印出id, weekday, description列中的数据。

while循环正在工作并打印出3个randoms行。但似乎完全忽略了开关盒。无论我选择周三或周六选择的形式,我都会在星期三和星期六之间得到混合印刷品。

表格的目的是,所以我可以单独看周三和周六。有没有人知道为什么会这样?

HTML

<select name="weekday" class="form-control selectpicker" >
            <option value=" ">Select Weekday</option>
            <option value="alldays">All Days</option>
            <option value="wednesday">Wednesday</option>
            <option value="saturday">Saturday</option>
    </select>
    <form method="post">
       <button class="btn btn-warning">Shuffle</button>
    </form>

PHP

<?php
    $sql = "SELECT id, weekday, description FROM articles";
      switch(isset($_POST['weekday'])){
        case 'wednesday':
            $sql .= " WHERE weekday LIKE 'Wednesday'";
        break;
        case 'saturday':
            $sql .= " WHERE weekday LIKE 'Saturday'";
        break;
      }
      $sql .= " ORDER BY RAND ( ) LIMIT 3";

      $res = $mysqli->query($sql);


    if ($res->num_rows > 0) {
        while($row = $res->fetch_assoc()) {
            echo "article Number: " . $row["id"]. "<br>" .
                 "Weekday: " . $row["weekday"]. "<br>" .
                 "Description: " . $row["description"]. "<br><br>".
                 '<div class="btn btn-warning btn-open-modal" data-id="'.$row["id"].'">See Details '.'</div><br><br>';              
        }
    } else {
    echo "0 results";
    }
?>

1 个答案:

答案 0 :(得分:2)

如果已设置,则开启,而不是实际值。检查它是否已设置,然后进入开关选项:

if(isset($_POST['weekday'])) {
  switch($_POST['weekday']){
    case 'wednesday':
        $sql .= " WHERE weekday LIKE 'Wednesday'";
    break;
    case 'saturday':
        $sql .= " WHERE weekday LIKE 'Saturday'";
    break;
  }
}