HomePHP Page 5 - Previous or Next? Paginating Records with PHP - Part 3
The final round: completing the "displayRecords()" method - PHP
In part three of our series about paginating records with PHP, we will learn how to make our application work with a relational database system such as MySQL. We will develop a tight, compact PHP class, capable of performing the most common paging tasks efficiently.
Completing the definition of this method is matter of adding to it the capabilities to handle any template file for formatting purposes, and generating the paging links. Actually, we're not so far from the finish line. Let's add the necessary code to work with template files and the paging links. Here are the additional lines to complete the method:
The majority of the listed code is fully commented and quite easy to understand. Let's begin by explaining how the method will format records using a template file. First, it reads the template file contents, storing them in the $templateContent array, like this:
Please, remember that the template file has three lines, defined as a header section, a placeholders section and a footer section. In order to format the records, the method first adds the header section to the final output, resetting the array pointer, and retrieving the first line of the template file, in the following way:
// add template header to final output
Now, it's time to move the pointers to the placeholders' line and replace them with the actual values:
After replacing the placeholders with the corresponding values, any unpopulated variables are removed from the final output, substituting the placeholders with an empty string, this way enhancing the class' flexibility. Finally, once records have been properly formatted, the footer line is appended to the output:
// add template footer to final output
That efficiently completes the process of formatting the records. As you can see, the approach is flexible enough to change the overall visual appearance, by just changing simple template files.
The rest of the code is nearly identical to the example listed in the second part, where the paging links are created by appending a <previous> link whenever possible, then generating the numbered links, and finally inserting a regular <next> link, when applicable. The only noticeable change hangs on providing an ID to tie a CSS style to the links, and offering a more polished appearance:
Having completed the definition for our "displayRecords()" class method, the class is already set up to be implemented on any existing or future Web application. Hopefully we've demonstrated that coding a PHP paging class is a not only an instructive experience, but also fun!
That's about it for the moment. In the final part of this series, we'll run through several examples to show how the class can be implemented in real-world applications, providing a useful way to introduce paginated result sets in websites. In the meantime, feel free to play with the class code, adding your own improvements to it.