Posts tagged ‘pagination’

PHP Pagination Simple Mechanics

It is actually very simple. All you need to know is what page you are on and how many items there is to list in total. Here is an out line:

Step one, get those values:
total_items
items_per_page
current_page

Step two, calculate total pages:
total_pages = ( total_items / items_per_page )

Step three, calculate limit offset for database query:
start_item = current_page * items_per_page
end_item = if on last page then ( total_pages – start_item ) else ( items_per_page ) “asuming your database works like mysql”

Be mindful that arrays and databases start from 0 not from 1.

And from there just make your PHP code and add on nifty features of your own.

Check out my previous guide for a nice pagination script with demo.

PHP Pagination Guide

At some point I needed to paginate my results of a large database for a more friendly view. Now I use PHP so this tutorial will fully explain how to paginate using php and SQL database, or just an array with all the data. That way you can use the technique with any other database you like. I will go over the basic mechanics and the add nice things on top like pagination and sorting at the same time and how to build a wicked page navigation for the listing. And also I shall show you how to make a so called server side pagination/sorting using Ajax JavaScript at the end. Continue reading ‘PHP Pagination Guide’ »