One handy class that Joe always keeps in his PHP toolbox is a custom MySQL recordset paging class, which he developed about 5 months ago. In this article Joe describes how he created the MySQL recordset paging class with PHP and also shows us how to use it with three cool examples!
Building A Dynamic MySQL Paging Class With PHP - The ShowRecs function (Page 4 of 6 )
ShowRecs is the one and only function contained within the RecNav class. It uses the variables setup by the constructor to interface with the MySQL database, generate the recordset pages, and also generate the paging links. Its signature looks like this:
function ShowRecs($Page=1)
It only accepts one parameter, $Page, which determines which page of the records we want to view. If $Page isn't passed in, then its given a default value of one. ShowRecs starts by creating a variable called $finalOutput, which will contain all of the template HTML code and records once the ShowRecs function finishes its work:
$finalOutput = $this->__templateHeader;
The MySQL query determines where to move the record pointer to and also how many records to return. The $Page variable is used in combination with the $__recsPerPage variable to build the query:
If $hasRecords is true then we loop through each record in the result using mysql_fetch_row. We also loop through the fields for each individual record using the mysql_num_fields function. We use str_replace to replace the placeholder values with the actual values contained within each row, like this:
$finalOutput will now contain the header template, $__templateHeader, as well as an instance of the $__template for each row in the result. On some occasions there may not be any rows returned from the query. To cover this, we replace the first occurance of a placeholder with the string "No records found". We also replace every other placeholder with a space by using the ereg_replace function:
else
{
// No records returned from the query
$newRow = $this->__template;
$newRow = str_replace("<| row0 |>", "No records found", $newRow);
Once the resultant HTML is built, we add the template footer to the $finalOutput variable:
$finalOutput .= $this->__templateFooter;
Notice the use of intrinsic documentation throughout the RecNav class? It’s always a good idea to name your variables after what they represent, such as $finalOutput in the code above. That way if another programmer has to work with your code, he won't have any trouble identifying what each variable does.
Creating the paging links
The ShowRecs function also creates the paging links which are appended to the $finalOutput variable. First off, the ceil function is used to determine the number of pages required to display the results. Ceil rounds a decimal number up, so 3.42 becomes 4 for example. Here's the code:
Next we have the main counter loop. This loop simply outputs the page number. If the value of the loop is the same number as the current page then that page number is output as bold. If not, the page number is linked using the $PHP_SELF variable and loop counter:
for($i = 1; $i < $numPages+1; $i++)
{
if($Page == $i)
{
// Bold the page and dont make it a link
$nav .= " <b>$i</b> |";
}
else
{
// Link the page
$nav .= " <a href='$PHP_SELF?page=$i'>$i</a> |";
}
}
The RecNav class is contained within a file called class.recnav.php. When you include this class in another PHP script and instantiate the RecNav class, then the value of $PHP_SELF will be the value of the script including class.recnav.php.
Once all of the pages are done, we include a "Next" link if possible: