将所有 While 输出回显到单个变量中
Echo all While outputs into a single variable
这是我的代码:
$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"
出于不同的原因,我真的很想将输出作为单个变量。
现在只从数据库输出第一个通知。
是否可以这样做:为 table 中的每一行回显 $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 that variable. Additionally you want to be aware that HTML inside a PHP variable may not always play nice. I would also recommend using heredoc 语法。它看起来像这样:
$notification .= <<<EOD
HTML goes here
EOD;
这是我的代码:
$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"
出于不同的原因,我真的很想将输出作为单个变量。
现在只从数据库输出第一个通知。
是否可以这样做:为 table 中的每一行回显 $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 that variable. Additionally you want to be aware that HTML inside a PHP variable may not always play nice. I would also recommend using heredoc 语法。它看起来像这样:
$notification .= <<<EOD
HTML goes here
EOD;