gpt4 book ai didi

php - 你如何在 while 循环之前回显内容?

转载 作者:太空宇宙 更新时间:2023-11-03 10:32:34 24 4
gpt4 key购买 nike

我正在尝试找出如何在 while 循环上方和检查 num_rows 下方回显内容,但我需要在执行此操作之前从 while 循环中获取内容。

$sql = "SELECT * FROM table WHERE column = ?";
$stmt = $db->prepare($sql);
$stmt->bind_param('s', $test);
$result = $stmt->execute();
$stmt_result = $stmt->get_result();
if ($stmt_result->num_rows > 0) {
echo "<div id='wrapper'>"; //I need to add HTML content here if $status === 2
while ($row = $stmt_result->fetch_assoc()) {
$title = $row['title'];
$description = $row['descript'];
$status = $row['status'];
if ($status === 2) {
echo $status;
continue; //skip to the next iteration
}
echo $title;
echo $description;
}
}

也许我错过了显而易见的事情。它是怎么做到的?

总而言之,这是我正在寻找的输出:

//if status !== 2: (i get 3 results)
<div id='wrapper'>
//title
//description

//title
//description

//title
//description
</div>
//if status === 2: (i get 1 result)
<div id='other_wrapper'>
//title
//description
</div>
//if status === 3: (i get 5 results)
<div id='yet_another_wrapper'>
//title
//description

//title
//description

//title
//description

//title
//description

//title
//description
</div>

最佳答案

也许这样的事情对你有用。

将这些行放在 while 循环的位置

$rows = $stmt_result->fetch_all(MYSQLI_ASSOC);
foreach ($rows as $row) {
if ($row['status'] === 2) {
$title = $row['title'];
$description = $row['descript'];
$status = $row['status'];
if ($status === 2) {
echo $status;
continue; //skip to the next iteration
}
echo $title;
echo $description;
}
}

关于php - 你如何在 while 循环之前回显内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55285827/

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