PHP Paginator

How many times have you had to implement some kind of pagination control? You know the whole, next, previous, page 1, page 2, page 3 links you place at the bottom of a list? Ten times? a hundred times? Whatever the number, it is probably a lot. So why are there not libraries out there that make generation of these links a breeze?

Now there is.

Screenshot

How it Works

The pagination model is based upon three key values that you pass into the main pagination routine:

  • the current offset in the list (how many items from the beginning of the list are you?
  • the number of items on each page, or "limit"
  • the total number of items in the result set

From these three key values one can generate a complete pagination control.

Functions/Methods

There is one static function in this class called paginate. It takes four parameters:

Parameters

  • offset - This indicates to the control what is the current page you are on. The value is the number of items you are from the beginning of the list.
  • limit - The number of items per page.
  • total - The total number of items in the list. This is used to compute the last page in the list.
  • base_url - This is the URL that is used for generating all the other URLs and links in the pagination control. Paginator will append to the end of this URL a number corresponding to the offset for that particular page.

Example Code

<?php
include("Paginator.php");
Paginator::paginate(0,100,10,"foo.php?bar=2&baz=3&offset=");
Paginator::paginate(11,100,10,"foo.php?bar=2&baz=3&offset=");
Paginator::paginate(21,100,10,"foo.php?bar=2&baz=3&offset=");
Paginator::paginate(31,100,10,"foo.php?bar=2&baz=3&offset=");
Paginator::paginate(41,100,10,"foo.php?bar=2&baz=3&offset=");
Paginator::paginate(51,100,10,"foo.php?bar=2&baz=3&offset=");
Paginator::paginate(61,100,10,"foo.php?bar=2&baz=3&offset=");
Paginator::paginate(71,100,10,"foo.php?bar=2&baz=3&offset=");
Paginator::paginate(81,100,10,"foo.php?bar=2&baz=3&offset=");
Paginator::paginate(91,100,10,"foo.php?bar=2&baz=3&offset=");
?>

Download

Resources

License

This code is licensed under the Free BSD license

Acknowledgements

Thanks to Logan Hansen for his contributions that made 1.01 possible.

4 Comments

Hi Byrne -

Looks interesting, to be sure. But if your arguments are offset, limit, total, baseurl, your example seems off, with what appears to be offset, total, limit, baseurl, yes? Shouldn't one or the other be changed?

Any examples of how you could use this in a MT template?

If you're going to use this with a MySQL database (which I presume is the most interesting usage for it), you must notice that you shouldn't be adding and substracting 1 to the offset, since MySQL does this for you. If you keep your class as it is, you'll be missing results!!!

There's a mistake in calculating the offset for the last 2 pages. Page $lastp should have an offset of ($lastp-1)$limit, and page $lastp-1 should have an offset of ($lastp-2)$limit.

Leave a comment

Close