gpt4 book ai didi

php - 如何从数据库导出值并回显所有由
分隔的值(第一个和最后一个结果除外)?

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

如何从数据库导出值并回显所有由


分隔的值(第一个和最后一个结果除外)?

$stmt = $db->prepare("SELECT `title`, `content` FROM `news` ORDER BY title ASC");
$stmt->execute(array($category));
foreach($stmt as $row){
echo "<p>" . $row['title'];
echo $row['content'] . "</p> <hr />";
}

它输出:

<p>title1
content1</p>
<hr>
<p>blabla
blabla</p>

<!--My question is how to remove this hr-->
<hr>

我尝试使用 for cicle,但没有成功。

提前致谢

最佳答案

你可以尝试这样的事情:

$stmt = $db->prepare("SELECT `title`, `content` FROM `news` ORDER BY title ASC");
$stmt->execute(array($category));

$numResults = $stmt->rowCount();
$counter = 0;
foreach($stmt as $row){
echo "<p>" . $row['title'];

if (++$counter == $numResults) {
// last row
echo $row['content'] . "</p>";
} else {
// not last row
echo $row['content'] . "</p> <hr />";
}
}

这将计算行数,直到最后一行,并跳过编写该行的 HR。

关于php - 如何从数据库导出值并回显所有由 <hr/> 分隔的值(第一个和最后一个结果除外)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34513031/

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