Php 将所有While输出回显到单个变量中
这是我的代码:Php 将所有While输出回显到单个变量中,php,if-statement,variables,mysqli,while-loop,Php,If Statement,Variables,Mysqli,While Loop,这是我的代码: $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 a
$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>
";
}
}
}
$sql=“从通知中选择*”;
if($result=mysqli_查询($link,$sql)){
如果(mysqli_num_rows($result)>0){
while($row=mysqli\u fetch\u数组($result)){
$notification=”
";
}
}
}
在我的页面中还有:
echo "<h6 class='dropdown-header'>Notificaties</h6>
$notification"
echo“通知
$notification“
出于不同的原因,我确实希望将输出作为单个变量
现在它只从数据库输出第一个通知
是否可以对表中的每一行执行如下操作:echo$通知?连接通知可能就可以了
$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> ";
$notification='';
while($row=mysqli\u fetch\u数组($result))
$notification.=”
";
看起来您正在为while循环中的每个实例编写$notification变量。你应该看看这个变量。此外,您还需要注意,PHP变量中的HTML可能并不总是很好地发挥作用。我还建议使用语法。它看起来像这样:
$notification .= <<<EOD
HTML goes here
EOD;
$notification.=将连接与$notification.=
现在显示更多,我收到了6个通知。奇怪的是,我的数据库中有两个通知。在之前的第83行,还得到了:Notice:Undefined variable:notification(注意:未定义变量):C:\xampp\htdocs\parameters.php中的通知,而您需要定义变量$notification=”
@timnijl并且您需要在使用变量之前对其进行初始化(在截取的部分添加了必要的行)“如何在循环中串联字符串”可能是堆栈溢出上的一个巨大重复@Berto请关闭而不是回答。非常感谢@Berto99!