Skip to main content

PHP Pagination Class

PS_Pagination 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

Update (1 July 2009): Fixed a bug. Added functionality to pass custom parameters.

Main Features

  • Ease of integration with existing PHP applications
  • Easy to customize. The main class is just 200 code lines
  • Append custom parameters to pagination links
  • Flexibility in rendering pagination links

Usage Instructions

Step One

The first step is to include the ps_pagination.php file in your script.

Step Two

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 while the last three are optional.

Step Three

Next, 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.

Step Four

The final step is to display the pagination links. You can 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.

//Include the PS_Pagination class

//Connect to mysql db
$conn = mysql_connect('localhost', 'username', 'password');

//Query to pass to pagination class
$sql = 'SELECT * FROM pages';

//Create a PS_Pagination object
$pager = new PS_Pagination($conn, $sql, 8, 3);

//The paginate() function returns a mysql result set for the current page
$rs = $pager->paginate();

//Loop through the result set just as you would loop
//through a normal mysql result set
while($row = mysql_fetch_assoc($rs)) {
echo $row['title'];

//Display the navigation
echo $pager->renderFullNav();

How do I use “WHERE”, “ORDER BY” with this class?

You don’t need to pass any special parameters for this to PS_Pagination object. Simply include these in your SQL statement. See below for example.

//Your SQL Query
$sql = "select title from product WHERE product_type = 'Digital' ORDER BY title ASC ";

//Create a PS_Pagination object and pass the above SQL statement to it
$pager = new PS_Pagination($conn, $sql, 8, 3, 'param1=valu1&param2=value2');

How do I get the total number of rows?

//Create a PS_Pagination object
$pager = new PS_Pagination($conn, $sql, 8, 3);

//Total number of rows returned
echo $pager->total_rows;

I want to append my own variables to the pagination links

You can pass your a variable string as the fifth argument while creating the pagination class object. This string will be appended to all pagination links.

//Create a PS_Pagination object
$my_variables = 'param_1=value_1&param_2=value_2';
$pager = new PS_Pagination($conn, $sql, 8, 3, $my_variables);

Functions Overview

  1. renderFirst – This function displays the link to the first page
  2. renderLast – This function displays the link to the last page
  3. renderNext – This function displays link to next page
  4. renderPrevious – This function displays link to previous page
  5. renderNav – Displays the page links
  6. renderFullNav – This function displays all the pagination links in one go

21 thoughts to “PHP Pagination Class”

  1. I just wanted to drop a line and thank you for you PHP pagination script. I’ve been using one for a while now that does not limit the amount of pages shown in the nav at one time, so I’ve had to limit my queries to 20 pages or so.

    I’ve searched and searched for one that does what yours does and they’ve all been extremely complicated. Yours is simple to use, understand and modify to fit my needs if necessary.

    I’m not a PHP newbie, but that particular solution has alluded me for some time.

    Thanks again,

  2. This is bar far the best pagination script i have found. Most others require you to do the coding within the query, but because this uses an external file, it saves the amount of php coding experience you need. World class and thank you for all your time in creation. A+++++

  3. It works great! Make sure when passing the sql statement, you don’t have a semicolon (;) at the end! I made that mistake and it took me a while time to figure it out.

  4. my question is regarding param1=value1&param2=value2. I would to substitute value1 and value2 with variables. i.e. param1=$orderby&param2=rank.

    How do I do that?

  5. @rick kuzyk

    The custom parameters are not for passing parmetes to the SQL query but to you own PHP script.

    If you want to use ORDER BY, add that to the SQL query (second parameter)

  6. could I use “limit” in my query….or how to show only X number of records (instead of 100 and 50 pages, 10 and 5 pages)…????

  7. thank you very much, i own couple of books about php&mysql and looked for this script in each of them, none contains number of pagination links option, basically most valued feature here…

    1. Dan, the renderNav function from the pagination class takes two optional parameters: prefix and suffix. You can pass any HTML tags through these parameters. These prefix and suffix are then wrapped around each of the page links.

      You can, for example, call:
      $pager->renderNav('<li>', '</li>');

      You can then apply CSS to customize the pagination links.

      Also take look at the source code of the renderFullNav function. It is very simply and you will understand how this function displays all the different parts of the navigation.

        1. Try the below code:

          <ul id="pagination-flickr">
          <li class="previous-off"><?php echo $pager->renderPrev();?></li>
          <?php echo $pager->renderNav('<li>', '</li>'); ?>
          <li class="next"><?php echo $pager->renderNext();?></li>

  8. The script serves well for a static query. But what if I build my query using post variables from a search/filter form ? The first page will be displayed correctly but the subsequent pages would return 0 rows since the POST variables no longer exist. To solve this, I did use session but then to do a new search, every time I will have to destroy the previous session. Any inputs on this please …?

    1. You don’t need to use session to store data. You can replace POST with GET variables and then simply pass long these variables as parameters using the pagination class.

  9. Hi Jatinder,
    thank you very much for your scripts!!!! I can easily integrate it to my PHP database list.
    Initially, i think it will be difficult for the integration as i got additional scripts to sort the database list according to the respective columns. But your instructions is very precised and easy to follow. I don’t need much effort in putting it to work with my scripts.

    I don’t even need to spend time debugging it.
    Once again, thank you very much for sharing! Really appreciates it! =)


  10. hello.. thank you for the codes and it’s working.. but I have a problem here, I used it for filtering data, it displays on the first page of what I’ve filtered but when I click the 2nd page, it’s selecting again all the queries I inserted, I want the second page will be the filtered.. how can I do that? please help me, it almost 2weeks solving this problem. thanks.. some others need to set the parameters but I dont know how to change. thanks

    1. Pauline, see the last section of this tutorial “I want to append my own variables to the pagination links”. You will need to pass the variables as query parameters and then fetch these variables using $_GET in your PHP script.

  11. I have the same problem Nirav had, i am using a post variable from a drop down menu for the query, which shows the first page of results.then error on the rest. only, i couldn’t quite understand your answer to him.
    ” You can replace POST with GET variables and then simply pass long these variables as parameters using the pagination class.”
    i understand the first part, but i don’t know what you meant by “pass long these variables as parameters using the pagination class.”
    could you give a written example of what u mean?, thankyou

    1. i understand the first part, but i don’t know what you meant by “pass long these variables as parameters using the pagination class.”
      could you give a written example of what u mean?, thankyou

      Dan, see the last section (I want to append my own variables to the pagination links) of the tutorial. You can append your own variables using the 5th parameter of the PS_Pagination constructor.

  12. now its 2013 and am still using your amazing class, couldn’t find anything wrong with it, I’ve tried to add my own styles and functions and still works well, hope you keep developing this class, to contain more awesome feature.

Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.