Reputation: 111
I have 100+ records in my database so i am using pagination but it prints page number for every page like 1,2,3,5 and so on how to limit the number shown in pagination.
Here is my code:
$row_offer2=mysqli_num_rows($sql_result2);
$total=ceil($row_offer2/$num_rec);
echo "<div class='col-md-12' style='margin:0 auto;text-align:center;'><ul
class='pagination '>";
echo "<li id='1' class='pag'>".'|<'."</li>"; // Goto 1st page
for ($i=1; $i<=$total; $i++) {
echo "<li id=".$i." class='pag'>".$i."</a></li> ";
};
echo "<li id=".$total." class='pag'>".'>|'."</a></li> "; // Goto last page
echo "</ul></div>";
?>
Upvotes: 0
Views: 114
Reputation: 9
Control your loop with number you want to show. If you want to show 3 pages in pagination just do this.
for ($i=1; $i<=3; $i++) {
echo "<li id=".$i." class='pag'>".$i."</a></li> ";
};
Upvotes: 0
Reputation: 682
Show pagination for current page +- x pages (x should be something like 2-3) and show the first and last page.
$currentPage = 6;
function getPages($cp, $max, $offset) {
$pages = [];
$pages[] = 1;
$min = ($cp - $offset) > 1 ? $cp - $offset : 1;
$lim = ($cp + $offset) < $max ? $cp + $offset : $max;
for ($i = $min; $i < $lim; $i++) {
$pages[] = $i;
}
$pages[] = $max;
return array_unique($pages);
}
var_dump(getPages($currentPage, 20, 3));
then you can simply loop with foreach through array you receive from this function and display your LIs
Upvotes: 1