I used mysqli_fetch_assoc function to fetch the rows in a decreasing order but when I echo the associative array of table "posts"(there are only three rows in the posts table- post 1,post 2 , post 3)
it prints in this manner:(I have added a screenshot of this output )
post 3 (edit/delete)
post 3 (edit/delete)
post 2 (edit/delete)
post 3 (edit/delete)
post 2 (edit/delete)
post 1 (edit/delete)
I wanted it to print in this manner:
post 3 (edit/delete)
post 2 (edit/delete)
post 1 (edit/delete)
Here is my code:
<?php
$db = mysqli_connect("localhost", "root", "", "myblog");
$sql = "SELECT * FROM posts ORDER BY id DESC";
$res = mysqli_query($db, $sql) or die(mysqli_error($db));
$count = mysqli_num_rows($res);
$posts = "";
if ($count > 0) {
while ($row = mysqli_fetch_assoc($res)) {
$id = $row['id'];
$title = $row['title'];
$content = $row['content'];
$date = $row['date'];
$admin = "
<div>
<a href='del_post.php?pid=$id'>DELETE </a>
<a href='edit_post.php?pid=$id'>EDIT</a>
</div>
";
$posts .= "<div><h4>$title</h4><h3>$date</h3><p>$content</p>$admin</div>";
echo $posts;
}
} else {
echo "there is nothing to show";
}
?>
Thanks.
I think the the problem is in this line,
while ($row = mysqli_fetch_assoc($res)
need to change $res for $count instead.