我不知道如何获取数据库中的所有数据并将其显示在通知菜单中,我显示的只是第一条记录。
这就是我现在拥有的一切。
HTML:
<a href="#">
<div class="user-img">
<img src="images/1 (2).png" alt="user" class="img-circle" name="imgs" id="imgs">
<span class="profile-status online pull-right"></span>
</div>
<div class="mail-contnet">
<h5 id="name"><?php echo $name; ?></h5>
<span class="mail-desc" id="msgs"><?php echo $msg; ?></span>
<span class="time" id="dTime"><?php echo $format; ?></span>
</div>
</a>
PHP:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "benchmark";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$name="";
$mail="";
$msg="";
$date = "";
$format = "";
$sql = "SELECT fullname, email, msg, time,date from msg";
$result = mysqli_query($conn,$sql);
$num = mysqli_num_rows($result);
while ($row = mysqli_fetch_array($result)) {
if ($num > 0) {
$name = $row["fullname"];
$mail = $row["email"];
$msg = $row["msg"];
$date = new DateTime($row["date"] . " " . $row["time"]);
$format = date_format($date, "Y-m-d g:i A");
}
else {
$name = "";
$mail = "";
$msg = "";
$date = "";
}
}
?>
答案 0 :(得分:1)
请检查以下代码
$output = '';
if($num > 0)
{
while ($row = mysqli_fetch_array($result))
{
$name = $row["fullname"];
$mail = $row["email"];
$msg = $row["msg"];
$date = new DateTime($row["date"] . " " . $row["time"]);
$format = date_format($date, "Y-m-d g:i A");
$output .= '<a href="#">';
$output .= '<div class="user-img"><img src="images/1 (2).png" alt="user" class="img-circle" name="imgs" id="imgs"> <span class="profile-status online pull-right"></span> </div>';
$output .= '<div class="mail-contnet"><h5 id="name">'.$name.'</h5> <span class="mail-desc" id="msgs">'.$msg.'</span> <span class="time" id="dTime">'.$format.'</span></div>';
$output .= '</a>';
}
}
echo $output;
答案 1 :(得分:0)
你的代码只是不断替换变量的值($ name,$ mail等)。您应该将其保存在增量变量中。
$data = '';
while ($row = mysqli_fetch_array($result)) {
if ($num > 0) {
$name = $row["fullname"];
$mail = $row["email"];
$msg = $row["msg"];
$date = new DateTime($row["date"] . " " . $row["time"]);
$format = date_format($date, "Y-m-d g:i A");
}
else {
$name = "";
$mail = "";
$msg = "";
$date = "";
}
$data.='<tr>
<td>'.$name.'</td>
<td>'.$mail.'</td>
<td>'.$msg.'</td>
<td>'.$date.'</td>
</tr>';
}
echo '<table>';
echo $data;
echo '</table>';