I’ve had a few pagination scripts over the years but I thought i’d share the one that i’m currently using as it’s a useful script to have in your website. As a developer you’ll soon find a need to paginate data when displaying contents from the database, and rather than use JavaScript which could require all the data to be loaded into the page on load, we can use PHP to ensure that we’re only requesting the data that we need from the database. For those who have no clue what i’m talking about. Pagination is a way of splitting up data into manageable chunks to display to a user, if you’ve spent more than two minutes on the internet chances are you’ve come into contact with some form of pagination. as a simple example you can checkout my website and allows you to split data up with it being paginated across however many pages dynamically.
1) { $pagination .= ""; // Previous if ($page > 1){ $pagination.= "previous"; }else{ $pagination.= "previous"; } // Pages if ($lastpage < 7 + ($adjacents * 2)) // Not enough pages to breaking it up { for ($counter = 1; $counter <= $lastpage; $counter++) { if ($counter == $page){ $pagination.= "$counter"; }else{ $pagination.= "$counter";} } } elseif($lastpage > 5 + ($adjacents * 2)) // Enough pages to hide a few? { // Beginning only hide later pages if($page < 1 + ($adjacents * 2)) { for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) { if ($counter == $page){ $pagination.= "$counter"; }else{ $pagination.= "$counter";} } $pagination.= "..."; $pagination.= "$LastPagem1"; $pagination.= "$lastpage"; } // Middle hide some front and some back elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) { $pagination.= "1"; $pagination.= "2"; $pagination.= "..."; for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) { if ($counter == $page){ $pagination.= "$counter"; }else{ $pagination.= "$counter";} } $pagination.= "..."; $pagination.= "$LastPagem1"; $pagination.= "$lastpage"; } // End only hide early pages else { $pagination.= "1"; $pagination.= "2"; $pagination.= "..."; for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) { if ($counter == $page){ $pagination.= "$counter"; }else{ $pagination.= "$counter";} } } } // Next if ($page < $counter - 1){ $pagination.= "next"; }else{ $pagination.= "next"; } $pagination.= ""; } echo $total_pages.' Results'; // pagination echo $pagination; ?>
- '.$row['country'].'
'; } ?>
Here’s the css to style you pagination