want to join us? register, it's easy | help

Simple PHP/MySQL Pagination

Code

Pagination Class

The full PHP code for the the pagination class has been put into the PHP Snippets section. You can find it here

How to use

$page = 1;
 
// how many records per page
$size = 10;
 
// we get the current page from $_GET
if (isset($_GET['page'])){
    $page = (int) $_GET['page'];
}
 
// create the pagination class
$pagination = new Pagination();
$pagination->setLink("list.php?page=%s");
$pagination->setPage($page);
$pagination->setSize($size);
$pagination->setTotalRecords($total_records);
 
// now use this SQL statement to get records from your table
$SQL = "SELECT * FROM mytable " . $pagination->getLimitSql();

To get the HTML code for the navigation list, you would simply call:

$navigation = $pagination->create_links();
echo $navigation; // will draw our page navigation

If you are familiar with CSS, you can easily style the way the page navigation by wrapping the HTML the class produces with a DIV. If you want to see a working example of this pagination, then simply look at the GoodPHPTutorials page navigation links, since we use this code for the site.