When displaying a large amount of content, pagination — splitting the content into separate pages — can improve usability. Pagination links allow the visitor to move between the different pages, but can make browsing difficult if implemented poorly. PaginationLinks is a PHP class that aids in creating effective pagination links.
Download PaginationLinks
Download the file below and upload it to your web server.
File | Size | Description |
---|---|---|
class.PaginationLinks.php | 3,715 bytes | PHP class |
Using PaginationLinks
Links can be created using the static create function of the PaginationLinks class. The function takes up to six parameters, but only two are required: the current page number, and the number of pages. For example:
1 2 3 4 5 |
|
This outputs the following links:
The links include the first page, the current page, the last page, and one page either side of each of these for context, with the omitted pages being represented by ellipses (the ‘...’ symbols). The amount of context can be controlled using the optional third parameter, which defaults to 1:
1 2 3 4 5 |
|
This outputs the following links:
Note that this list includes only a single ellipsis as there are no omitted pages between the first page and current page.
Customising the HTML
When called with two or three parameters, the create function produces HTML in the following format:
1 2 3 4 5 6 7 8 9 |
|
Each page link sets the GET parameter page to the page number, the current page number is within a span element, and omitted pages are represented by the character entity reference for an ellipsis.
The remaining three parameters can be used to override this default formatting. The fourth and fifth parameters provide templates for the links and the current page respectively. These are used as parameters for the PHP sprintf function, and so occurrences of %d will be replaced by the page number. The sixth parameter provides a string to use as the ellipsis. For example:
1 2 3 4 5 6 7 8 9 10 11 |
|
This outputs the following links: