HomePHP Page 4 - Previous or Next? Paginating Records with PHP, part 1
Putting the "paginateRecords()" function into action: procedural record paging - PHP
Putting all of your content for a particular file on one page can be very user unfriendly to website visitors. Nobody likes to be confronted by a tiny vertical scroll bar! In this article, the first in a series, you will learn a simple way to paginate records from a text file using PHP.
Let's jump straight in and test out our recently developed "paginateRecords()" function. With three lines of code, we're ready to paginate records on our Web pages. Here's a possible regular implementation:
Let's be honest. The required code to make record paging is extremely short and understandable. We've just included the file that contains the function and invoked it, passing in the parameters the data file to be read and the page pointer to display the records. Just give it a try and add your own improvements. Simple and straightforward, isn't it?
However, there are some issues to be considered about this approach. This solution might be suitabel for small websites, where you can live quite happily delivering content from simple text files. With larger applications, however, and with heavy loads and more demanding requirements, this approach falls short. What's more, we're mixing HTML markup with PHP code, which is not recommended at all.
So, can we do it any better? You bet. We must improve this technique by developing a solution definitely more portable for larger websites, and able to deal with a relational database system such as MySQL. That will be our next step in the paging record process.
Summarizing and further improvements
In this first part, we looked at a simple PHP function for paginating records extracted from a flat text file. Also, we saw how simply it can be implemented, with just a few lines of code. Of course, this procedural method supports many possible improvements, being a handy introduction to paging techniques. If you're already working with Web applications that require recordset paging very frequently with minor headaches, just take the function and play with it to meet your needs.
In the coming second part, we'll look at a mature OOP approach, defining a PHP class to efficiently handle recordset paging processes, considerably more suitable for larger applications. It sounds very promising. We have the tools and the knowledge for developing such a class. So, get ready for the next part, and stay tuned!