Home arrow PHP arrow Page 6 - Previous or Next? Paginating Records with PHP - Part 2

Pager class implementation - PHP

In the first article in this series, we learned how to do simple pagination of records taken from a text file. In this article, we will look at pagination for a larger group of records, using an Objected Oriented rather than a procedural approach to creating the application.

  1. Previous or Next? Paginating Records with PHP - Part 2
  2. Taking the procedural approach: the friendly "paginateRecords()" function
  3. Taking the OOP approach: defining the "Pager" PHP class
  4. Defining the "displayRecords()" method
  5. Source code ahead: completing the "displayRecords()" method
  6. Pager class implementation
By: Alejandro Gervasio
Rating: starstarstarstarstar / 10
May 03, 2005

print this article



With the class completely defined, it's easy to implement a couple of examples to show how it can used. However, let's first show the complete list for our Pager class. This is the full source code:

class Pager {

var $output='';

var $totalRecs;

var $numRecs;

function Pager($dataFile,$numRecs=5){

// validate data file

('Data file not valid.');

// validate number of records per page

>numRecs=$numRecs:die('Invalid number of records


function displayRecords($page){

// calculate number of pages


// validate page pointer




// retrieve corresponding records from flat file


foreach($records as $row){


foreach($columns as $column){



$this->output.='<br />';


// create previous link


$this->output.='<a href="'.$_SERVER['PHP_SELF'].'?


// create intermediate links


($i!=$page)?$this->output.='<a href="'.$_SERVER


// create next link


$this->output.='&nbsp;<a href="'.$_SERVER
['PHP_SELF'].'?page='.($page+1).'">Next&gt;&gt;</a> ';


// return final output

return $this->output;



Having listed the complete class code, we can show our first example, which is presented in the following way:


$pager=&new Pager('data.dat');


echo $pager->displayRecords($page);

That's it. With four lines of code, we're displaying records from the text file "data.dat" and displaying the corresponding paging links, using a default value of five records per page.

If that's not good enough for you, here's another example, showing records from a text file "data2.dat", in sets of 10 records at a time:


$pager=&new Pager('data2.dat',10);


echo $pager->displayRecords($page);

Our newly developed class is very flexible and portable, and easy to implement in different situations, simply by changing the parameters passed to it. As usual, feel free to play with the code and tweak it to fit your particular needs. Certainly, the possibilities are numerous.


In this second part, we've developed in a step-by-step process, a PHP class to paginate records in websites. Noticeably, we've branched out to an OOP approach to illustrate the great advantages that classes offer to PHP programmers. However, before you start clamoring for more complex paginating solutions, we're not done yet.

In its current incarnation, the class lacks many desirable features that should be present in real-world applications. The first clear issue is that we're working with text files as a data source. If we're going to work with medium to large sites, we'll quickly encounter a relational database system. The second problem is the basic mechanism presented to apply some kind of visual format to the records. Thus, all of these relevant topics should be properly addressed without modifying the inner structure of the class, while maintaining the basics of the original code.

In the third part of this series, we'll apply some interesting improvements to the class, making it possible to work directly with MySQL, offering a flexible mechanism for defining the records' visual presentation. What more can we ask for? In the meantime, have some fun playing with the class. See you soon!

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