我想学习如何查询一次并在多个框中显示 where子句?
喜欢:
如果状态为1? :显示为<div class="1">
elseif status 2吗?:<div class="2">
等,等等。
我知道我的答案是我的问题,我需要做if语句,但是我不知道如何在同一查询中显示while循环或foreach几次。
这是我的查询:
$stmt = $pdo->prepare("SELECT * FROM categories");
$stmt->execute();
while($row = $stmt->fetch()){
}
我在Google上搜索过,其中有一些带有for循环的示例,但没有找到带有claus的示例。
$i;
$stmt = $pdo->prepare("SELECT * FROM categories");
$stmt->execute();
while($row = $stmt->fetch()){
}
for($i=0; $i<5; $i++){
}
我尝试了没有foreach循环的陈述:
if($status == 1){
foreach(){
echo "Display box 1";
}
}
if($status == 2){
foreach(){
echo "Display box 2";
}
}
在循环或foreach中仅显示一个帖子需要显示状态为1的所有帖子 谢谢
答案 0 :(得分:0)
我不知道我是否理解你的问题,但是:
$stmt = $pdo->prepare("SELECT * FROM articles");
$stmt->execute();
while($row = $stmt->fetch()){
echo "<div class=\"class_". $row["status"] ."\">div content</div>\n";
}
而且-CSS类不能以数字开头或仅以数字作为名称!在我的示例中,我使用.class_x(其中X是您从数据库中获取的值)
答案 1 :(得分:0)
虽然您可以缓冲多个流,将整个数据集读取到PHP数组中并进行搜索或运行多个查询,但这一切都是不必要且效率低下的。只需对查询进行排序。考虑:
$stmt = $pdo->prepare("SELECT * FROM categories ORDER BY status");
$stmt->execute();
$prevstatus=-1;
print "<div style='display:none'>";
while($row = $stmt->fetch()){
if ($row['status']!=$prevstatus) {
$prevstatus=$row['status'];
print "</div><div class='$prevstatus'>";
}
print ....
}
print "</div>";