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)

13 thoughts on “PHP Pagination Class – Version 3”

  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";
    }
    }

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>