Reputation:
I don't know how to get all the data in my database and display it in the notification menu, all i have displayed is the first record.
This is all what i have now.
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 = "";
}
}
?>
Upvotes: 0
Views: 47
Reputation: 872
You're code just keep replacing the value of your variables ($name, $mail, etc.). You should save it in an incremental variable.
$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>';
Upvotes: 0
Reputation: 1039
Please check below code
$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;
Upvotes: 1