HomePHP Page 3 - Previous or Next? Paginating Records with PHP - Part 2
Taking the OOP approach: defining the "Pager" PHP class - 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.
There are still many heated discussions about the pros and cons of Procedural and Object Oriented Programming in PHP. Regardless of the different opinions voiced about the topics, generally developers seem to agree on one point: OOP is best suited for medium or larger projects, while a procedural approach works better for small applications. Nothing stops you from applying the method that best fits your particular needs. However, with an extremely versatile language such as PHP, as Web applications evolve from simple scripts to full-fledged programming structures, the OOP approach is definitely the right way to go.
Keeping in mind the concepts explained above, we'll tackle the record paging issue, creating a PHP class to handle records in an efficient manner. Our "Pager" class will present only two methods: the corresponding class constructor and the "displayRecords()" method, which, not surprisingly, will display the records and create the paging links. Let's start by defining the blueprints for the class:
class Pager {
var $output;
var $totalRecs;
var $numRecs;
function Pager($dataFile,$numRecs){
// code for parameters setup
}
function displayRecords($page){
// code to display records and paging links
}
}
As you can see, the structure of the class is very simple, and displays three properties: $output, $totalRecs and $numRecs, respectively. The first property will store progressively the output generated, including record values and paging links. The second one represents the total records retrieved from the data file, and finally the third argument shows the number of records per page being displayed. Indeed, the class structure is pretty self-explanatory.
Once we've defined the skeleton of the class, it's time to add some functionality to the constructor. Let's take a look at its complete definition:
function Pager($dataFile,$numRecs=5){
// validate data file
(file_exists($dataFile)&&count(file($dataFile))>0)? $this->totalRecs=array_reverse(file($dataFile)):die ('Data file not valid.');
// validate number of records per page
(is_int($numRecs)&&$numRecs>0)?$this- >numRecs=$numRecs:die('Invalid number of records '.$numRecs);
}
As you can deduce from the code for the constructor, it accepts only two parameters: the data file and the number of records to be shown per page. In this particular case I've decided to give a default value of five records, but as usual, could be changed by passing another value to the method.
Next, the constructor checks to see whether the data file is valid and not empty. If the file is considered valid, then the constructor gets the complete records from the data file, assigning them as a class property. Otherwise, the class is stopped with a die() statement.
What is the constructor's next task? It validates the number of records per page, by checking to see whether the value passed is a positive integer. If the condition is meet, the method assigns the value as another class property. If the value is not valid, the class is simply killed using a regular die() statement.
Notice the simplistic nature of the constructor, which performs the validation of parameters and sets up the corresponding properties. Once the method has completed its tasks, we need to define the other method of the class: "displayRecords()". Let's take a deep breath and jump straight into its code definition.