In this article, attached the script to make it working and easy to edit. This is a flexible and easy to implement PHP pagination script which will help you split large result sets over multiple pages. Click here to download the script
Steps to use:
- include the ps_pagination.php file to your script.
- Create a ps_pagination object. The ps_pagination class constructor takes five 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
- Your own custom parameters you want to be appended to pagination links
- The first two are compulsory and the last three are optional’s.
- Call the paginate() function. This function returns a paginated result set for the current page. You can use this result set just as you would use a normal MySQL result set.
- To display the pagination links, use the renderFullNav() function to generate and display the links in one go or you can use individual function calls to display each link separately. You can also use this functions to display the paginate links:
- renderFirst – This function displays the link to the first page
- renderLast – This function displays the link to the last page
- renderNext – This function displays link to next page
- renderPrevious – This function displays link to previous page
- renderNav – Displays the page links
- renderFullNav – This function displays all the pagination links in one go
Example:
<?php
//Enable & display error messages
ini_set(‘display_errors’, ‘On’);
error_reporting(E_ALL);//Include the PS_Pagination class
include(‘ps_pagination.php’);// database connection config
$dbHost = ‘localhost’;
$dbUser = ‘root’;
$dbPass = ”;
$dbName = ‘mydb’;// database Connection
$dbConn = mysql_connect ($dbHost, $dbUser, $dbPass) or die (‘MySQL connect failed. ‘. mysql_error());
mysql_select_db($dbName) or die(‘Cannot select database. ‘. mysql_error());$sql = ‘select post_title from posts’;
$rows_per_page = 10;
$links_to_display = 5;
$extra_parameters = “param1=value¶m2=value”;//Create a PS_Pagination object
$pager = new PS_Pagination($dbConn, $sql, $rows_per_page, $links_to_display, $extra_parameters);$rs = $pager->paginate();
//Loop through the result setwhile($row = mysql_fetch_assoc($rs)) {
echo $row[‘post_title’].”<br/>”;
}
//Display the navigation
echo $pager->renderFullNav();?>