<!-- Step-1 : add pagination php calculaiton code form user.php file -->
<!-- Step-2 : add pagination php next previous and selected code form user.php file -->
<!-- Step-3 : add while php loop code for fetching and display data form DB fomr users.php file -->
<!-- Step-4 : use join for fetching catagory-name from catagory table & author name form role table using their respective ids -->
<!-- Step-5 : use specific table name insted of * for safety cocern -->
<!-- Step-6 : if normal user loign all post form all usern should not be dispaly to him -->
<?php include "header.php"; ?>
<div id="admin-content">
<div class="container">
<div class="row">
<div class="col-md-10">
<h1 class="admin-heading">All Posts</h1>
</div>
<div class="col-md-2">
<a class="add-new" href="add-post.php">add post</a>
</div>
<div class="col-md-12">
<!-- Step-1 : add pagination php calculaiton code form user.php file -->
<?php
include "config.php";
$limit = 3;
if(isset($_GET['page'])){
$page = $_GET['page'];
}
else{
$page = 1;
}
$offset = ($page-1)*$limit;
/*-- Step-4 : use join for fetching catagory-name from catagory table & author name form role table using their respective ids */
/* $sql = "SELECT * FROM post
LEFT JOIN category ON post.category = category.category_id
LEFT JOIN user ON post.author = user.user_id
ORDER BY post.post_id DESC LIMIT {$offset},{$limit}"; */
/*-- Step-5 : use specific table name insted of * for safety cocern --> */
/*-- Step-6 : if normal user loign all post form all usern should not be dispaly to him --> */
if($_SESSION["user_role"] == '1'){
$sql = "SELECT post.post_id, post.title, post.description, post.post_date,
category.category_name, user.username FROM post
LEFT JOIN category ON post.category = category.category_id
LEFT JOIN user ON post.author = user.user_id
ORDER BY post.post_id DESC LIMIT {$offset},{$limit}";
}elseif($_SESSION["user_role"] == '0'){
$sql = "SELECT post.post_id, post.title, post.description, post.post_date,
category.category_name, user.username FROM post
LEFT JOIN category ON post.category = category.category_id
LEFT JOIN user ON post.author = user.user_id
WHERE post.author ={$_SESSION['user_id']} /*-- Step-6 */
ORDER BY post.post_id DESC LIMIT {$offset},{$limit}";
}
$result = mysqli_query($conn, $sql) or die("Query Failed.");
if(mysqli_num_rows($result) > 0){
?>
<table class="content-table">
<thead>
<th>S.No.</th>
<th>Title</th>
<th>Category</th>
<th>Date</th>
<th>Author</th>
<th>Edit</th>
<th>Delete</th>
</thead>
<tbody>
<!-- Step-3 : add while php loop code for fetching and display data form DB fomr users.php file -->
<?php
while($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td class='id'><?php echo $row['post_id'] ?></td>
<td><?php echo $row['title'] ?></td>
<td><?php echo $row['category_name'] ?></td>
<td><?php echo $row['post_date'] ?></td>
<td><?php echo $row['username'] ?></td>
<td class='edit'> <a href='update-post.php?id=<?php echo $row['post_id'] ?>'> <i class='fa fa-edit'> </i></a></td>
<td class='delete'> <a href='delete-post.php?id=<?php echo $row['post_id'] ?>'> <i class='fa fa-trash-o'> </i></a></td>
</tr>
<?php } ?>
</tbody>
</table>
<?php
}
$sql1 = "SELECT * FROM post";
$result1 = mysqli_query($conn, $sql1) or die("Query Failed.");
if(mysqli_num_rows($result1) > 0){
$total_records = mysqli_num_rows($result1);
/* $limit = 3; // put it on the top */
/*-- Step-2 : add pagination php next previous and selected code form user.php file --> */
$total_page = ceil($total_records / $limit);
echo "<ul class='pagination admin-pagination'>";
if($page > 1){
echo '<li><a href="post.php?page='.($page - 1).'">Prev</a></li>';
}
for ($i = 1; $i <= $total_page; $i++)
{
if($i==$page){
$active = "active";
}
else{
$active = "";
}
echo'<li class="'.$active.'"><a href="post.php?page='.$i.' ">'.$i.'</a></li>';
}
if($total_page > $page){
echo '<li><a href="post.php?page='.($page + 1).'">Next</a></li>';
}
echo "</ul>";
}
?>
</div>
</div>
</div>
</div>
<?php include "footer.php"; ?>