Simple PHP/MySQL Pagination Class
February 17th, 2007 at 10:26 am (PHP Articles, MySQL Articles)
In this article you will learn a simple method to make PHP/MySQL Pagination Class.
What is Pagination?
==============
What if you have a table with a thousand rows, and you want to allow
the user to browse through the entire table. Simply listing all the
records in that table would not be a good idea. Instead you should
break the table up into smaller “chunks” and allow the user to
navigate through theses “chucks”. This is what pagination does, it
allows you to break up large result sets from a database query, and
present it to the user in a more manageable way.
Implementing pagination
=================
Pagination Class
————————-
The full PHP code for the the pagination class has been put into the
Good PHP Tutorials Snippets section. You can find it here:
http://www.goodphptutorials.com/snippet/show/12
How to use
—————–
<?php
$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 out pagination class
$pagination = new Pagination();
$pagination->setLink("list.php?page=%s");
$pagination->setPage($page);
$pagination->setSize($size);
$pagination->setTotalRecords($total_records);
$SQL = "SELECT * FROM mytable " . $pagination->getLimitSql();
// now use this SQL statement to get records from your table
?>
To get the HTML code for the navigation list, you would simply call:
<?php
$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 Good PHP Tutorials
page navigation links, since we use this code for the site.









