PHP Pagination Class

January 16th, 200721 Comments »

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.

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.

How do I get the total number of 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.

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
« Sending emails in PHP & email injection attacks
Truncate long string along word boundaries »

Categorized Under

PHP & MySQL PHP Development

About jatinder

» has written 17 posts

21 Comments

  1. Shawn says:

    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,
    Shawn

  2. Raymond says:

    Very nice pagination class!

  3. Michael Thomson says:

    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+++++

  4. Joel says:

    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.

  5. rick kuzyk says:

    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?

  6. Jatinder says:

    @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)

  7. gustavo says:

    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)…????

  8. Jatinder says:

    @gustavo

    Sorry, you can’t use “limit” in your query. “limit” is already being used internally by the pagination class.

  9. JanT says:

    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…

  10. Dan Kelly says:

    Is there a way to change the Pagination links to images instead? Well, maybe just customize the links..

    • jatinder says:

      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.

  11. Nirav says:

    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 …?

    • jatinder says:

      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.

  12. Alvin Poon says:

    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! =)

    Regards,
    Alvin

  13. Pauline says:

    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

    • jatinder says:

      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.

  14. dan says:

    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

    • jatinder says:

      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.

  15. OsaMa says:

    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.


*

Popular Posts