The PHP Paging Class
Next up is our Paging Class. This class will be used when we wan to create a paging layout. With Paging layout I mean when we have many records we want to display, but we don’t want to display them all at the same time. We need to create something like this: Back 1 [2] [3] [4] [5] Next. If we manage to do that, we are able to toggle between the different ‘pages’ but we are actually on the same PHP page all the time, we are just getting different record information from the database depending on which ‘page’ we currently visit.
Due to the fact that writing this paging code every time you want to use the paging functionality you need to code a lot of code, code you probably have written before but to reuse it you need to modify a few lines. Having code slightly similar or exactly the same on several pages is bad. Try making it into a function or, as in this case, a class. We want to be able to reuse the code we write as much as possible. Sounds weird? Hopefully you will get the hang of it when we go through the script.
OBS! The PHP Paging Class has been updated with a smoother code. The guide below will go through the basic functionallity of the Paging Class but the download might differ from the guide!
Now we are off to see what the Paging Class might look like! We are looking at 8 different functions inside the class, don’t worry they are rather small. To start off, we take a look at the declared private class variables:
We have $result, $pagesize, $page, $rs, $param and $db. The $result will hold a MySQL result set, the result set we are going to use when we toggle between the different pages. In the $pagesize variable we are able to set how many records we want to display on each page, say 25. The $page variable will hold the current page number for us. $rs is the counter making sure we are not accessing more records than the $pagesize variable let us. Lastly we have the $param variable. When we toggle via our pages we need to provide a variable telling our script on which page we want to go. The $param variable is the name of that variable. Oh and I almost forgot, the $db variable holds our Database Class instance.
Next up is our __construct() function. It takes 4 parameters: a SQL query, the page size, the name of the result variable and lastly the database instance. Inside this function we are assigning our private class variables with our incoming parameters. The class variable $result is using our database instance and our supplied SQL query to fetch a MySQL result set. We are also making a few checks. The current page number we are on needs to have a value larger than 0. If the current page number is larger than the highest page number available we are fixing that. We are using the get_num_pages() to retrieve the number of pages our result will require with that specific page size and lastly we call a function called set_page_num() with the current result page as an argument.
Okay, let’s take a look at the get_num_page() function. The function is, as I said, used to determine how many pages our result will need. We make sure we haveĀ a result set to work with, otherwise we return false. If a result is found we use the newly created num() function in the database class to fetch the total number of pages we will need. Finally before returning that number, we use the built-in function ceil so we are 100% sure that we don’t get 6,5 pages when we need 7 to display every record correctly.
The set_page_num() function take a parameter: $pagenum. This page number comes from the __construct function. We make sure again that the page number isn’t bigger than the highest possible value for a page number and that the page number is bigger than 0. After that we assign the private class variable $page the value of $pagenum. We also set the class variable $rs to 0 and finally call our data_seek() function with our result and which row the MySQL pointer should start at.
This is going great! We have already covered 3 functions, 5 to go! Inside the set_page_num() function we are calling another function get_num_pages(). This function has only one job, to return the private class variable $page. Next is_last_page() and is_first_page(). These small functions will return true if the current page is the last resp. first page.
Only two more to go! Now we have functionality to find the number of pages we need, if the current page is first, current or last. Now we want to be able to fetch a row from the result, we will do this by declaring our fetch() function. The function will make sure that the current number of displayed records ($rs) is smaller than the page size and that we have a result to work with. If this is true we will add 1 to the private class variable $rs and lastly return a record object from our database using the database function fetch_object().
The last function in the Paging Class is get_links(). So far we are not able look at anything. We have lot’s of code but nothing is showing anything. With our get_links() function we will retrieve the links I was talking about in the beginning, the Back 1 [2] [3] [4] [5] Next. Here will we use the private class variable $param again and we are also able to provide more parameters to the function. This is done be adding a string of key and values.
Finally finished! Or wait a minute? We need some sort of example on how to use this. We need to include both our database class file and our new paging class file into our default PHP page. First we create an instance of our database class providing the necessary arguments (database name, server name, user and password). After that we can create our instance of the Paging class. We supply our SQL query, the number of records shown per page, the name of the result variable and lastly the database instance so our Paging class can work independently towards the database. We use a while loop to loop through our records. After that we call our get_links() function and echo those out.
So there we have it. We are able to create a paging layout using about 5 lines of code! Now we are able to add as many paging layout’s as we want, we can reuse our Paging Class. And that was exactly what we wanted! Reduce code and reused code! All good! Comments, questions and suggestions are, as always, very welcome! Tomorrow we take a look at the Record Class which will help us retrieve and saving information in a MySQL database without having to use ANY SQL commands, it’s all done from our class. Until then, have a good day!
Update
About this entry
You’re currently reading “The PHP Paging Class,” an entry on Is the coffee still warm?
- Published:
- June 22, 2008 / 10:06 am
- Category:
- June







7 Comments
Jump to comment form | comments rss [?] | trackback uri [?]