<!-- Step-1: fetch only data from catagory which has number of post -->
<!-- Step-2: make the selected link higlited -->
<!-- Step-3: send category id and page id both on pagination links -->
<!-- Step-4: display category name on the category page -->
<?php include 'header.php'; ?>
<div id="main-content">
<div class="container">
<div class="row">
<div class="col-md-8">
<!-- post-container -->
<div class="post-container">
<?PHP
/*-- Step-1: fetch only data from catagory which has number of post --> */
$sql1 = "SELECT * FROM category WHERE category_id = {$cat_id}";
$result1 = mysqli_query($conn, $sql1) or die("Query Failed.");
$row1 = mysqli_fetch_assoc($result1);
?>
<!-- Step-4: display category name on the category page -->
<h2 class="page-heading"><?php echo $row1['category_name'];?></h2>
<?PHP
include "config.php";
if(isset($_GET['cid'])){
$cat_id = $_GET['cid']; /* get id from the url on catagory click */
}
$limit = 3;
if(isset($_GET['page'])){
$page = $_GET['page'];
}
else{
$page = 1;
}
$offset = ($page-1)*$limit;
$sql = "SELECT post.post_id, post.title, post.description, post.post_date,
category.category_name, user.username, post.category,post.post_img FROM post
LEFT JOIN category ON post.category = category.category_id
LEFT JOIN user ON post.author = user.user_id
WHERE post.category = {$cat_id}
ORDER BY post.post_id DESC LIMIT {$offset},{$limit}";
$result = mysqli_query($conn, $sql) or die("Query Failed.");
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_assoc($result)) {
?>
<div class="post-content">
<div class="row">
<div class="col-md-4">
<a class="post-img" href="single.php?id=<?php echo $row['post_id']; ?>"><img src="admin/upload/<?php echo $row['post_img']; ?>" alt=""/></a>
</div>
<div class="col-md-8">
<div class="inner-content clearfix">
<h3><a href='single.php?id=<?php echo $row['post_id']; ?>'><?php echo $row['title']; ?></a></h3>
<div class="post-information">
<span>
<i class="fa fa-tags" aria-hidden="true"></i>
<!-- Step-3: send category id and page id both on links -->
<a href='category.php?cid=<?php echo $row['category']; ?>'><?php echo $row['category_name']; ?></a>
</span>
<span>
<i class="fa fa-user" aria-hidden="true"></i>
<a href='author.php'><?php echo $row['username']; ?></a>
</span>
<span>
<i class="fa fa-calendar" aria-hidden="true"></i>
<?php echo $row['post_date']; ?>
</span>
</div>
<p class="description">
<?php echo substr($row['description'],0,130) . "..."; ?>
</p>
<a class='read-more pull-right' href='single.php?id=<?php echo $row['post_id']; ?>'>read more</a>
</div>
</div>
</div>
</div>
<?php
}
}else{echo "<h2>no record found </h2>";}
?>
</div><!-- /post-container -->
<?php
/* put the above code at the top */
/* $sql1 = "SELECT * FROM category WHERE category_id = {$cat_id}";
$result1 = mysqli_query($conn, $sql1) or die("Query Failed.");
$row = mysqli_fetch_assoc($result1);
*/
if(mysqli_num_rows($result1) > 0){
$total_records = $row1['post'];
$total_page = ceil($total_records / $limit);
echo "<ul class='pagination admin-pagination'>";
if($page > 1){
echo '<li><a href="index.php?cid='.$cat_id.'&page='.($page - 1).'">Prev</a></li>';
}
/*-- Step-2: make the selected link higlited --> */
for ($i = 1; $i <= $total_page; $i++)
{
if($i==$page){
$active = "active";
}
else{
$active = "";
}
/*-- Step-3: send category id and page id both on links --> */
echo'<li class="'.$active.'"><a href="index.php?cid='.$cat_id.'&page='.$i.' ">'.$i.'</a></li>';
}
if($total_page > $page){
echo '<li><a href="index.php?cid='.$cat_id.'&page='.($page + 1).'">Next</a></li>';
}
echo "</ul>";
}
?>
</div>
<?php include 'sidebar.php'; ?>
</div>
</div>
</div>
<?php include 'footer.php'; ?>