Here is a better example of how to do pagination of data in PHP.
Let’s say you have a database table full of animals. You only want to show 10 animals per page, with “previous” and “next” buttons to allow users to go to the previous or next page of results.
There are a few different things we need to keep track of in our code to make this type of pagination work. Here’s a quick run-down of the most important pieces of code in the controller:
- $pageNum = the page number of the page the user is viewing
- $numRowsPerPage = the number of results we want to show on each page
- $startIndex = the index of the first row we want to display on this page
- $endIndex = the index of the last row we want to display on this page
- $numRowsTotal = the total number of results in the database
- $numPagesTotal = the total number of pages of results
The current page number
The current page number is retrieved from the query string of the URL using the ubiquitous $_REQUEST variable.
//FIGURE OUT WHICH PAGE OF RESULTS TO SHOW
$pageNum = $_REQUEST['page']; //which page to show
//if no page requested, load the first page
if (empty($pageNum)) {
$pageNum = 1;
}
This code checks for the page number in the query string. If there is no page number there, it defaults to page 1.
The number of results to show on each page
The $numResultsPerPage variable is just hardcoded to some value:
//PAGINATION SETTINGS
$numRowsPerPage = 10; //how many results to show per page
The index of the first row to show
If the user is viewing page 1, and there are 10 rows per page, then the first row we want to show them is row 1, and the last row is row 10. If they are on page 2, then the first row we want to show them is row 11, and the last is row 20… because they have already seen 1-10.
The general formula for this is:
//calculate the start index of the rows to show on this page
$startIndex = ($pageNum-1) * $numRowsPerPage; // get the starting index number of the first item to show on this page
The index of the last row to show
In general, the index of the last row on the page is just the index of the first row, plus the number of rows on the page.
//calculate the end index
$endIndex = $startIndex + $numRowsPerPage;
However, on the very last page, it may be that there are fewer than 10 results. This means that the index of the last row may not be just the index of the start row plus 10. So we need to check to make sure that if we are on the last page, the value we calculated for the index of the last row is not greater than the total number of rows in the table.
//make sure on the last page that we don't have an end index that is greater than the total number of rows
if ($endIndex > $numRowsTotal) {
$endIndex = $numRowsTotal;
}
The total number of rows
The total number of rows is just how many rows there are in the database table.
$numRowsTotal = sizeof(getAnimals()); //get the total number of animals in the database
This function getAnimals() returns an array with the full list of animals, and we use PHP’s built-in sizeof() function to get the number of elements in that array.
The total number of pages
The total number of pages is just the total number of rows, divided by the number of results per page. That’s the math.
$numPagesTotal = ceil($numRowsTotal / $numRowsPerPage); //get the total number of pages
Display the number of results
Assuming the aforementioned variables have all been set up properly in the PHP in your controller script, you will probably want to display the indexes of the results shown on any given page, as well as the total number of results somewhere in the XHTML template you are using for the view of your application.
The following bit of XHTML interspersed with “template-style” PHP will display the start and end number of the results on the page, as well as the total number of results in a nicely formatted, “normal” way that is commonly used by search engines:
<span>Displaying <?php echo $startIndex + 1 ?>-<?php echo $endIndex ?> of <?php echo $numRowsTotal ?> results</span>
This will output something like this in the browser,
Displaying 1-10 of 35 results
Display links to other pages
Let’s say you have 10 pages of results. The first time a user comes to your site, they will see page #1. So obviously, in your XHTML templates, you will also want to dislpay links to the other pages of results. To do this, you will make use of some of the other variables we have set up in the PHP controller script.
Here is an example of how to use the PHP variables to output a very ordinary set of links to other pages:
<?php if ($pageNum > 1) : ?> <a href="index.php?page=<?php echo $prevPage ?>">Prev</a> <?php endif ?> <?php if (($pageNum-1) * $numRowsPerPage + $numRowsPerPage < $numRowsTotal) : ?> <a href="index.php?page=<?php echo $nextPage ?>s">Next</a> <?php endif ?> <?php for ($i=1; $i<=$numPagesTotal; $i++) : ?> <a <?php if ($pageNum == $i) : ?>class="selected"<?php endif ?> href="index.php?page=<?php echo $i ?>"><?php echo $i ?></a> <?php endfor ?>
Let’s take this code line-by-line. The first three lines here display the “Prev” link to the previous page:
<?php if ($pageNum > 1) : ?> <a href="index.php?page=<?php echo $prevPage ?>">Prev</a> <?php endif ?>
If the user is on the first page, it doesn’t make sense to have a link to the previous page since there is no previous page, so we check to make sure the user is not on the first page before displaying this link.
The next three lines of code display the link to the “Next” page:
<?php if (($pageNum-1) * $numRowsPerPage + $numRowsPerPage < $numRowsTotal) : ?> <a href="index.php?page=<?php echo $nextPage ?>s">Next</a> <?php endif ?>
This code is basically just making sure that there is a next page before outputting the link. It doesn’t make sense to show a link to the next page if we’re already on the last page.
And these last three lines display the page numbers of all the pages as links:
<?php for ($i=1; $i<=$numPagesTotal; $i++) : ?> <a <?php if ($pageNum == $i) : ?>class="selected"<?php endif ?> href="index.php?page=<?php echo $i ?>"><?php echo $i ?></a> <?php endfor ?>
The for loop here loops through from the first to the last page, and displays each page number as a dynamic link to that page of results. It highlights the current page by adding a CSS class called “selected” to the link tag.
No related posts.