PHP Pagination Script
As a developer there will come a time when we need to fetch huge amount of records to display on a single page. An example is when you’re trying to develop an e-commerce site and you need to output lets say 500 book titles. Another example would be in a member management type of system wherein you need to list all of the registered members of the site. What you need to do is not list all of the records in a single page but rather split the results into pages because users are too lazy to scroll down. This is where pagination comes into play.
I’m not going to reinvent the wheel by coming up with another pagination script instead I will suggest that you checkout and try PHP Pagination Script from PHPSense. Its a PHP class that is very easy to integrate with your existing applications by following these few simple steps:
Step One: Include the pagingation class on the document that will output the long list of records.
View CodePHP | |
//Include the Pagination class include('ps_pagination.php'); | |
Step Two: Create the pagination object. It expects four parameters:
- MySQL connection link
- SQL query
- Number of records to display per page. Defaults to 10
- Number of pagination links to display. Defaults to 5
View CodePHP | |
include('ps_pagination.php'); //Connection to MySQL database $conn = mysql_connect('localhost', 'username', 'password'); mysql_select_db('yourdatabase', $conn); $query = "SELECT * FROM yourtable"; //Create the PS_Pagination object $pager = new PS_Pagination($conn, $query, 10, 5); | |
Step Three: After creating the pagination object, call the paginate() function from the object. This function returns the paginated result set for the current page. You can use this result set just as you would use a standard MySQL result set.
View CodePHP | |
include('ps_pagination.php'); //Connection to MySQL database $conn = mysql_connect('localhost', 'username', 'password'); mysql_select_db('yourdatabase', $conn); $query = "SELECT * FROM yourtable"; //Create the PS_Pagination object $pager = new PS_Pagination($conn, $query, 10, 5); $results = $pager->paginate(); //Loop through the result set while ($row = mysql_fetch_array($results)) { echo $row['yourfield']; } | |
Last step: Display the pagination links by calling the renderFullNav() function.
View CodePHP | |
include('ps_pagination.php'); //Connection to MySQL database $conn = mysql_connect('localhost', 'username', 'password'); mysql_select_db('yourdatabase', $conn); $query = "SELECT * FROM yourtable"; //Create the PS_Pagination object $pager = new PS_Pagination($conn, $query, 10, 5); $results = $pager->paginate(); //Loop through the result set while ($row = mysql_fetch_array($results)) { echo $row['yourfield']; } //Display the navigation echo $pager->renderFullNav(); | |
As you can see its very simple to use in your applications. Paginating pages is one of the most commonly requested functions in websites and this is one of the many scripts that can really help you with your projects. I have been looking for a pagination script for a while now and by far this is the best and most efficient pagination script I’ve used. As always feel free to suggest if you’re using any pagination scripts. Hoping to learn from you guys. Thanks for reading.
Comments
Leave a Reply