gpt4 book ai didi

php - switch case 在查询中被忽略

转载 作者:行者123 更新时间:2023-11-29 06:02:54 25 4
gpt4 key购买 nike

我有一个在 switch case 中设置的查询。 while 循环遍历我的行,并打印出 3 个随机行,其中 id, weekday, description 列中的数据被打印出来。

while 循环正在运行并打印出 3 个随机行。但似乎开关盒完全被忽略了。无论我在选择表单中选择星期三还是星期六,我都会得到星期三和星期六的混合打印。

表格的目的是让我可以分类为仅分别查看星期三和星期六。有人知道为什么会这样吗?

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

最佳答案

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

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

关于php - switch case 在查询中被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43726515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com