Home arrow PHP arrow Page 2 - Previous or Next? Paginating Records with PHP, part 4

A quick look at the class source code - PHP

In the fourth and final article in our series about paginating records with PHP, we will look at the source code for the "pager" PHP class we developed in part three, and show several examples to demonstrate possible uses in production environments.

  1. Previous or Next? Paginating Records with PHP, part 4
  2. A quick look at the class source code
  3. Creating the database table
  4. Paging MySQL records: putting the "Pager" class into action
  5. Extending the use of the "Pager class": more examples to come
By: Alejandro Gervasio
Rating: starstarstarstarstar / 16
May 17, 2005

print this article



Before we try the class and show a real example, it's necessary to list its full code. This way, you'll have an overall conception of how it works. Here's the complete source code for the "Pager" class:

* PHP Pager class
* @access public

class Pager {
  * MySQL connection identifier
  * @access private
  * data type string
  var $conId;


  * SQL query
  * @access private
  * data type string
  var $query;


  * number of records per page
  * @access private
  * data type integer
  var $numRecs;


  * generated output for records and paging links
  * @access private
  * data type string
  var $output;


  * records template file
  * @access private
  * data type string
  var $template;


  /** ID attribute for styling paging links
  * @access private
  * data type string
  var $linksId;


  * Pager constructor
  * @access public
  * @param string conId
  * @param string query
  * @param string linksId
  * @param integer numRecs
  * @param string template
  function Pager (&$conId,$query,$linksId,$numRecs=10,

    // validate connection idenfifier
    (mysql_query('SELECT 1',&$conId))?$this->conId=
     &$conId:die('Invalid connection identifier.');

    // validate query
     $query:die('Invalid query '.$query);

    // validate paging links ID
     $linksId:die('Invalid ID for paging links '.$linksId);

    // validate template file
     $template:die('Invalid template file '.$template);

    // validate number of records per page
      $numRecs:die('Invalid number of records '.$numRecs);

    // initialize output



  * method displayRecords
  * @access public 
  * @return string output
  * @param integer page
  * @description display paginated records/paging links
  function displayRecords($page){

    // calculate total number of records
      die('Cannot retrieve records from database');

    // calculate number of pages

    // get result set
    $result=mysql_query($this->query.' LIMIT '.($page-1)*$this->numRecs.','.$this->numRecs);

    // read template file

    // append template file footer to final output

    // move pointer to placeholder line

    // replace placeholders with actual data

      // remove unpopulated placeholders


    // append template file footer to final output

    // create page links
    $this->output.='<div id="'.$this->linksId.'">';

    // create previous link
      $this->output.='<a href="'.$_SERVER['PHP_SELF'].'?page='.

    // create numerated links
      ($i!=$page)?$this->output.='<a href="'.$_SERVER['PHP_SELF']

    // create next link
      $this->output.='&nbsp;<a href=
       '.($page+1).'">Next&gt;&gt;</a> ';

    // return final output
    return $this->output;





That was a long listing, don't you think? However, it's really worthwhile. Now we're ready to develop some examples and see the class in action. Since the class will work with the MySQL server, let's begin by setting up a sample database and creating a basic table with some records. Want to know more? Just click the link and keep reading.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: