I'm a web developer, freelancer, author, speaker, entrepreneur, technical reviewer and blogger based in the North East, living in a village just outside of Chester-le-Street.
My blog
Making Pagination Easier
Pagination is one of those repetetive tasks which developers hate to do. Quite a while ago I made a nice function to handle most of my pagination tasks, and return an array of data I could do something with, to make things easier developing at work. More recently, I've improved this into a nice little object of its own, which I've modified slightly for release here.
My pagination object works by taking a limit of how many results should be displayed per page, and offset to determine which page we are on, and a query to paginate. A mysqli connection is passed to the constructor, so it can access the database (I do this differently for my own usage, but for the sake of re-use, I've tweaked it to work this way.
Usage
// establish a database connection in your code
$db = mysqli_connect( 'localhost', 'root', '', 'database');
// create the pagination object, and pass the database connection (assumes class is included)
$p = new Pagination( $db );
// set a limit
$p->setLimit(5);
// set an offset
$p->setOffset( isset( $_GET['page_number'] ) ? intval( $_GET['page_number'] ) : 0 );
// set the query
$p->setQuery( "SELECT * FROM table " );
// do the pagination
$p->generatePagination();
// what we do with these variables depends on the tempating system used: the pagination object could send them direct to the template system for us
// get an array of results
$results = $p->getResults();
// get the current page number
$current_page = $pagination->getCurrentPage();
// get the number of pages
$number_of_pages = $pagination->getNumPages()
if( $pagination->isFirst() )
{
$first_page_link = "";
$previous_page_link = "";
}
else
{
$first_page_link = "<a href='?page_number=0'>First page</a>";
$previous_page_link = "<a href='?page_number=".( $pagination->getCurrentPage() - 2 )."'>Previous page</a>";
}
if( $pagination->isLast() )
{
$next_page_link = "";
$last_page_link = "";
}
else
{
$next_page_link = "<a href='?page_number=" . $pagination->getCurrentPage() . "'>Next page</a>";
$last_page_link = "<a href='?page_number=".( $pagination->getCurrentPage() - 1 )."'>Last page</a>";
}
Download
You can either download the class and supporting files as a zip, or view the code online.
Donate
Like this code, want to give me some pennies?
Posted by Michael on 14th Apr 2010 at 11:11
Comments
Would... $p->setQuery( "SELECT * FROM table " ); Not be quite intensive on a large database? I tend to use SELECT COUNT(*) AS count ... to get the total number of records from a database table, but I'm no expert so not sure if that saves any resources or not.
Posted by Martin Bean on 16th April 2010 at 14:02
That's a fair point, and one which surprisingly I've not actually considered. Perhaps some regex would be in order to replace everything between SELECT and FROM for the query and replacing it with count(*) for running it the first time, and storing the results from that as the total. Thanks for the comment Martin!
Posted by Michael Peacock on 16th April 2010 at 19:07


