PHP Pagination Class – Version 3

It’s been a while since the popular PHP pagination class has received any of my attention, so a few months ago I set out to re-write the class to be more up to date. This version takes advantage of both code and security updates as well as updates to PHP and MySQL. I’m also planning on moving the code over to GitHub where it should be easier to manage.

Here is the new and improved PHP pagination class for your enjoyment:


See the example files for how to use. I’ll create step-by-step instructions shortly.


Note that the demo uses MySQL’s free “world” database available at

  • Example 1 (basic paginated database result set)
  • Example 2 (Form variables passed via pagination class)

21 thoughts on “PHP Pagination Class – Version 3”

      1. The Description of my problem that
        I used this class in my own simple MVC
        My Link before use class was
        public/home/index(or any other method)
        After i click on the link of class
        public/home/index/page2(or any num)?route=home/index/page2(or any num)
        and more on other hit on link

        i used url method
        in my .httacess
        i use
        Options -MultiViews
        RewriteEngine On

        RewriteBase /mvc_ad/

        RewriteCond !-d
        RewriteCond !-f
        RewriteRule ^$ index.php?route=home [QSA,L]
        RewriteRule ^(.+)$ index.php?route=$1 [QSA,L]

        to remove route from url
        but it has been appear when used pagination class

        can u help me??

  1. This is a cool pagination class but I have a problem with this 3rd updated version. I can’t really use more than one instance of the pagination class on my site. In my design, I have one class for connections to the database. I can’t figure out how to define an instance of the pagination class in say database.php and use it in users.php to paginate the users gotten from the database. When I define $pages = new Paginator($num_rows,9,array(15,3,6,9,12,25,50,100,250,’All’)); in database.php and call $pages->display_pages(); in users.php, it return an error! Maybe I’m missing something!

  2. How do I select more than one table., I tried this but does not work

    $stmt = $conn->prepare(‘SELECT COUNT(*) FROM Shop1,Shop2, Shop3 WHERE ProductCategory1 =:ProductCategory1 AND ProductCategory2=:ProductCategory2 AND ProductCategory3=:ProductCategory3 AND Price Between :PR1 AND :PR2′);
    $stmt->bindParam(‘:PR1′, $PR1, PDO::PARAM_INT);
    $stmt->bindParam(‘:PR2′, $PR2, PDO::PARAM_INT);
    $stmt->bindParam(‘:ProductCategory1′, $ProductCategory1, PDO::PARAM_STR);
    $stmt->bindParam(‘:ProductCategory2′, $ProductCategory2, PDO::PARAM_STR);
    $stmt->bindParam(‘:ProductCategory3′, $ProductCategory3, PDO::PARAM_STR);
    $num_rows = $stmt->fetchColumn();

  3. Extend the pagination class … to show the range of values being shown
    First add two public variables:
    public $limit_range_start;
    public $limit_range_end;

    After this line (about line 82):
    $this->limit_start = ($this->current_page current_page-1) * $this->items_per_page;

    insert the following three lines:
    $limit_between = ($this->total_items-$this->limit_start items_per_page) ? $this->total_items-$this->limit_start : $this->items_per_page;
    $this->limit_range_start = $this->limit_start + 1;
    $this->limit_range_end = $this->limit_start + $limit_between == 0 ? $this->total_items : $this->limit_start + $limit_between;

    Then add the following function at the end:
    public function display_range(){
    return “Showing “.$this->limit_range_start.” to “.$this->limit_range_end.” of “.$this->total_items.” entries”;

    This will show something line:

    Showing 1 to 10 of 11 entries

  4. Hello,
    I’m trying to get sum values in array in side a $_POST but I had this message:
    Notice: Array to string conversion in D:\xampp\htdocs\news\lib\class_paginator.php on line 50

    how to solve this?

  5. Hello,
    The problem is on line 50 they add any $_POST to $this->querystring, it’s wrong you have cheek this!.

    if($_POST) {
    foreach($_POST as $key=>$val) {
    if($key != "page" And $key != "ipp") $this->querystring .= "&$key=$val";

  6. I see that the code for V3 includes references to both GET and POST. But the page buttons that seem to be generated always have URLs that look as though they are intended to be processed by code using the GET method. What if the code at the receiving end is set to use the POST method?… does anything special need to be done in this case?

  7. Recommended update to prevent Array to string conversion warnings
    if($_POST) {
    foreach($_POST as $key=>$val) {
    if($key != "page" And $key != "ipp") $this->querystring .= "&$key=$val";

    if($_POST) {
    foreach($_POST as $key=>$val) {
    if($key != "page" And $key != "ipp") $this->querystring .= "&$key=$val";

  8. Ran across this site while searching for help with some pagination. Little did I know this one class would solve my problems. Thanks for a wonderful class that works great, even with my convoluted code ;)..

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>