HomePHP Page 5 - Previous or Next? Paginating Records with PHP - Part 2
Source code ahead: completing the "displayRecords()" method - 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.
Let's jump directly into the code to complete the method definition. Once the page pointer has been properly validated, it's time to fetch the exact number of records specified for being displayed on each page. Since we've already stored the total number of records as a class property $this->totalRecs, the process of extracting the corresponding records is a breeze. We just use the array_slice() PHP function to position the array internal pointer at the right place, and retrieve the correct number of records. This simple line extracts the records from the $this-> totalRecs array, as listed below:
Now, if the page pointer $page is equal to 1, and assuming that the default number of records is 5, the $records variable will store the first five elements from the $this->totalRecs array. Similarly, if $page is equal to 2, the second set of five records will be stored. As you can see, the expression is simple but extremely powerful for extracting chunks of records, closely resembling the LIMIT SQL statement extensively used with MySQL.
At this point, the method only needs to loop over the extracted records and progressively generate the output to be displayed in the browser. Using a couple of "foreach" loops, the task is nicely performed:
foreach($records as $row){
$columns=explode('|',$row);
foreach($columns as $column){
$this->output.=$column.' ';
}
$this->output.='<br />';
}
By now, we've applied very basic formatting to the records. However, this might be changed with minor modifications. Before I hear someone complaining about this, in Part III of this article, we'll cover in-depth record formatting to suit real-world needs. So, you don't have any excuses to skip the next part of the series!
The rest of the code is nearly identical to the example shown in the first part. The methods take care of creating the paging links, first checking whether it's possible to insert a <previous> link. If it's possible, the corresponding link is generated. Then, the numbered links are created using a loop, and appending the $page variable to the querystring, providing the necessary navigational links.
Finally, the methods verifies whether it's feasible to generate a <next> link, to completely display the paging links.
Here are the lines used to create the paging links:
We've retrieved the records to be displayed and built the paging links with a single method. Definitely, the method shows its power and simplicity with some lines of code. Maybe we're not ready to paginate thousands of records, but this attempt is fairly valid!
Well, our job is almost done by now. But, am I forgetting anything? Sure, we need to put the class into action and see its actual functionality. Therefore, let's write a bit more of code to implement our "Pager" class.