PHP: Simple Pagination
This is a simple pagination snippet I wrote for a Drupal module I was working on. I’ve “unDruaplized” it and now it’s completely independent.
The function will calculate the number of pages to display based on
$shown. In addition, it'll append the corresponding page number to the provided
This function takes 4 params:
$total- total number of results.
100in our example
$page- the page number you're currently on. using
$_GET['p']in our example
$shown- number results to display per page.
10results per page in our example
$url- the url each link should have.
?p=in our example
100(results) / 10(per page) = 10(pages)
NOTE: All page numbers are incremented by one. This means, the function assumes URL 0 as page 1.
echo handle_pagination(100, (int)$_GET['p'], 10, '?p=');
The following is an example of the markup our snippet below will generate
Which should produce something like the following:
1 2 3 4 5 ... next › last »