如何解决将所有While输出回显到单个变量中
这是我的代码:
$sql = "SELECT * FROM notifications";
if($result = mysqli_query($link,$sql)) {
if(mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result)) {
$notification = "
<a class='d-flex align-items-center dropdown-item' href=".$row["link"].">
<div class='dropdown-list-image mr-3'><i class='fas fa-bell'></i>
<div class='bg-success status-indicator'></div>
</div>
<div class='font-weight-bold'>
<div class='text-truncate'><span>".$row["omschrijving"].".</span></div>
<p class='small text-gray-500 mb-0'>".$row["klant"]." - ".$row["createdat"]."</p>
</div>
</a>
";
}
}
}
在我的页面中还有:
echo "<h6 class='dropdown-header'>Notificaties</h6>
$notification"
出于各种原因,我真的很想将输出作为单个变量。
现在,它仅从数据库输出第一个通知。
是否可以这样做:对表中的每一行都回显$ notification?
解决方法
串联通知可能会做到
$notification = '';
while($row = mysqli_fetch_array($result))
$notification .="
<a class='d-flex align-items-center dropdown-item' href=".$row["link"].">
<div class='dropdown-list-image mr-3'><i class='fas fa-bell'></i>
<div class='bg-success status-indicator'></div>
</div>
<div class='font-weight-bold'>
<div class='text-truncate'><span>".$row["omschrijving"].".</span></div>
<p class='small text-gray-500 mb-0'>".$row["klant"]." - ".$row["createdat"]."</p>
</div>
</a> ";
,
好像您正在为while循环中的每个实例覆盖$ notification变量。您应该查看concatenating该变量。另外,您还需要注意,PHP变量中的HTML可能并不总是能很好地发挥作用。我也建议使用heredoc语法。看起来像这样:
$notification .= <<<EOD
HTML goes here
EOD;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。