Reputation: 61
We are working on the pagination part for a blog, we are have some issue, the issue is when we click on next it is going to the last page and when we are going to click on previous its going to point to first page. While we are clicking on page numbers its working correctly.
Can some body help us to fix the issue of the next and previous button.
Thanks a lot for your time, the PHP Coding as follows.
<?php
if(isset($_REQUEST['category_id']))
{
$cat_id_page=$_REQUEST['category_id'];
$sql = "SELECT * FROM blog where category_id='$cat_id_page' ORDER BY entry_date DESC";
}
else
{
$sql = "SELECT * FROM blog ORDER BY entry_date DESC";
}
$rs_result = mysql_query($sql); //run the query
$total_records = mysql_num_rows($rs_result); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
echo '<div class="space x2">
<div class="blog-pagination">';
echo '<a class="w-clearfix w-inline-block button btn-small btn-blog" href="blog.php?page=1#">
<div class="btn-txt">Previous</div>
</a>';
if(isset($_REQUEST['page']))
{
$page_id=$_REQUEST['page'];
}
else
{
$page_id=1;
}
for ($i=1; $i<=$total_pages; $i++) {
if($page_id==$i)
{
//echo "<li class=active><a href='blog.php?page=".$i."'>".$i."</a></li>";
echo '<a class="w-clearfix w-inline-block button btn-small btn-blog active" href=blog.php?page='.$i.'>
<div class="btn-txt">'.$i.'</div>
</a>';
}
else
{
//echo "<li><a href='blog.php?page=".$i."'>".$i."</a></li>";
echo '<a class="w-clearfix w-inline-block button btn-small btn-blog" href=blog.php?page='.$i.'>
<div class="btn-txt">'.$i.'</div>
</a>';
}
};
//echo '<li><a href=blog.php?page='.$total_pages.'>Next Page<i class="fa fa-long-arrow-right"></i></a></li>';
echo '<a class="w-clearfix w-inline-block button btn-small btn-blog" href=blog.php?page='.$total_pages.'>
<div class="btn-txt">Next</div>
</a>';
echo '</div></div>';
?>
Upvotes: 0
Views: 63
Reputation: 344
for previous button;
$current_page - 1
for next button :
$current_page + 1;
seems to the selected / current page is $page_id so ;
if($page_id != 1)
echo ' <a href="?page='.($page_id-1).'">< Previous</a> ';
if($page_id != $total_pages)
echo ' <a href="?page='.($page_id+1).'">Next ></a> ';
Upvotes: 1