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:

Download

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

Demo

Note that the demo uses MySQL’s free “world” database available at http://dev.mysql.com/doc/index-other.html

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

27 thoughts on “PHP Pagination Class – Version 3”

      1. The Description of my problem that
        I used this class in my own simple MVC
        So,
        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
        and
        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);
    $stmt->execute();
    $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
    Changing:
    if($_POST) {
    foreach($_POST as $key=>$val) {
    if($key != "page" And $key != "ipp") $this->querystring .= "&$key=$val";
    }
    }

    to
    if($_POST) {
    foreach($_POST as $key=>$val) {
    if(!is_array($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 ;)..

  9. Hi there,
    Great work, thanks so much for this. I used it for a custom php project and it works perfectly.
    But i’m having problem when i try to use it in a wordpress project.
    When i try to include this class in wordpress everything stops when wordpress reache the include line.
    Any idea why?
    Thanks in advance.

  10. I just got it to work. There is a name conflict in wordpress (probably there is an other class with the same name). I just changed the class name for Paginator to MyPaginator and everything is working fine now. Thanks again.

  11. My previous comment was not absolutely correct. The class is working but not the page links.
    There are some more things i have to change to make it work all the way.
    I’ll be back when i’m done (i hope…)
    Any help from anyone would be much appreciated
    Thanks any way…

  12. Back again.
    As i said previously, i wonna use this class in a custom query (custom table) in wordpress. After a lot of search and effort i found out that this class works perfectly in wordpress it you use default permalinks but not if you change them and use custom permalinks (postname, custom structure etc).
    Any idea why this is happening??
    Can anybody help please, i’m stucked in dead end here…
    Thanks :(

  13. I had forgot this one. Any way, better late than never.
    I finally got it work in wordpress and permalinks.

    1) I changed the name of the variable $page to $mypage, or something like that, cause there was a conflict. Looks like wp has a variable with the same name.

    2) I changed $_SERVER[PHP_SELF] with this variable ( $cur_plink_url ) where ( $cur_plink_url = get_permalink() )

    Hope this helps anyone who wants to use this class in wordpress.

    Again, thanks for sharing thiw class.

Leave a Reply

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