<!-- Step-1: open main index.php file and delete extra div -->
<!-- Step-2: copy pagination calculation code form post.php file -->
<!-- Step-3: copy sql command form post.php and add image query code -->
<!-- Step-4: c-p sql run query form post.php file -->
<!-- Step-5: c-p while loop code form post.php file -->
<!-- Step-6: c-p pagination code form post.php file -->
<!-- Step-7: replace the dummy data with the fetched data in row array variables -->
<!-- Step-8: code for dynamic image fetch -->
<!-- Step-9: code for click on image more or title of the post to the page > single.php -->
<!-- Step-10: use substr for showing less text on the main 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-2: copy pagination calculation code form post.php file --> */
include "config.php";
$limit = 3;
if(isset($_GET['page'])){
$page = $_GET['page'];
}
else{
$page = 1;
}
$offset = ($page-1)*$limit;
/*-- Step-3: copy sql command form post.php and add image query code --> */
$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
ORDER BY post.post_id DESC LIMIT {$offset},{$limit}";
/*-- Step-4: c-p sql run query form post.php file --> */
$result = mysqli_query($conn, $sql) or die("Query Failed.");
if(mysqli_num_rows($result) > 0){
/*-- Step-5: c-p while loop code form post.php file --> */
while($row = mysqli_fetch_assoc($result)) {
?>
<div class="post-content">
<div class="row">
<div class="col-md-4">
<!-- Step-8: code for dynamic image fetch -->
<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">
<!-- Step-7: replace the dummy data with the fetched data in row array variables -->
<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>
<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">
<!-- Step-10: use substr for showing less text on the main page -->
<?php echo substr($row['description'],0,130) . "..."; ?>
</p>
<!-- Step-9: code for click on image more or title of the post to the page single.php -->
<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 -->
<!-- Step-6: c-p pagination code form post.php file -->
<?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 */
$total_page = ceil($total_records / $limit);
echo "<ul class='pagination admin-pagination'>";
if($page > 1){
echo '<li><a href="index.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="index.php?page='.$i.' ">'.$i.'</a></li>';
}
if($total_page > $page){
echo '<li><a href="index.php?page='.($page + 1).'">Next</a></li>';
}
echo "</ul>";
}
?>
</div>
<?php include 'sidebar.php'; ?>
</div>
</div>
</div>
<?php include 'footer.php'; ?>