PHP - Pagination made easy

From Global Programming Syntax

Jump to: navigation, search

When displaying pages, often pagination will need to be used. A lot of people may ask, what is pagination and why would I need to use it. Well the answer is simple and clear. Pagination is basically splitting a group of information into separate pages to make it more readable. An example is a comments box. If you have a thousand comments on a single page on your website then your page will take for ever to load. However if you have links down the bottom to page 1, 2, 3, 4 etc where the comments are split across then the user the choose which section to view so if they visit a second time they can jump to the section they were at. Also by using pagination you will reduce the serverload/cpu usage on your server and at the same time reduce your bandwidth when used properly. That is why the top 3 search engines only show the top 10 results on the first page. To save serverload/cpu and bandwidth.

<?php
$pages_left=3;
$pages_middle=4;
$pages_right=2;
 
$total_pages=100;
if (!isset($_GET['page']) && empty($_GET['page'])) {
$currentpage=0;
} else {
$currentpage=$_GET['page'];
}
for ($p=0;$p<=$total_pages;$p++) {
if ($p<$pages_left || $p>=($total_pages-$pages_right) || ($p>=($currentpage-floor($pages_middle/2)) && $p<=($currentpage+floor($pages_middle/2)))) {
if ($p==$currentpage) {
echo ($p>0)?' - ':'';
echo '<b>'.($p+1).'</b>';
} else {
echo ($p>0)?' - ':'';
echo '<a href="index.php?page='.$p.'"><b>'.($p+1).'</b></a>';
}
}
}
?>

Also to use pagination once the above code is applied you will find that it links to a page with the url variable page=30 for example. To use that data to display the page you simply put the $_GET['page'] into the mysql limit statement like the following:

mysql_query('SELECT * FROM `table` LIMIT '.mysql_real_escape_string($_GET['page']).', 1');

Understanding the Code

<?php
$pages_left=3;
$pages_middle=4;
$pages_right=2;
 
$total_pages=100;

In this section of code is the php opening tag at the top then below that is four variables. Each of these variables will control how the pagination will display. The first variable controls how many page numbers the count from the beginning then the second variable is how many page numbers to have in the middle and the last variable is how many page numbers to have at the end of the row. Then at the bottom is the total number of pages that can possibly be displayed. You will find that the total pages variable will usually contain the value of mysql_num_rows() for the selection of all rows.

if (!isset($_GET['page']) && empty($_GET['page'])) {
$currentpage=0;
} else {
$currentpage=$_GET['page'];
}

This section of code basically validates the current page number so that if the current page number is not in the url then the current page number will be set to zero.

for ($p=0;$p<=$total_pages;$p++) {

This is the loop to display all of the page numbers. Basically it loops through all of the pages and displays the required page numbers. Also the $p variable is being used to determine what the current page is for the loop.

if ($p<$pages_left || $p>=($total_pages-$pages_right) || ($p>=($currentpage-floor($pages_middle/2)) && $p<=($currentpage+floor($pages_middle/2)))) {
if ($p==$currentpage) {
echo ($p>0)?' - ':'';
echo '<b>'.($p+1).'</b>';
} else {
echo ($p>0)?' - ':'';
echo '<a href="index.php?page='.$p.'"><b>'.($p+1).'</b></a>';
}
}

The first if statement will determine if the page is to be displayed. You shouldn't have to worry about understanding this if statement as it can be configured by changing the variables at the very beginning. Then the second if statement checks if the current page is the same as the page which is being checked. So for example if currently page 50 is displayed and the loop is currently on number 50 then it will pass that if statement and only display the text instead of the link. If the script passes the second if statement then a dash will be display only when $p is greater than zero else it will echo nothing. Same for when it both does and doesn't pass that second if statement. Then there is of course the link to each other page which is being displayed. Also to prevent page number zero from being displayed the number one is added to the current page when displaying it.

Video Tutorial for above code

Click the below video to view.
[video src=php_pagination size=600x474]

Personal tools
languages
page stats
Toolbox