And here we are in the series on my GitHub Projects, with PHP-Pagination.
What is this?
Oh, pagination.
This library provides a clean, decoupled "API" for developers to integrate
pagination markup into their site or
application.
It provides simple methods to control the settings for the generated pagination markup, including the number of items per page, the total number of items and more.
Why did I develop it?
Well, that's a good question. There are plenty of pagination scripts out there, but they all felt increasingly complicated for what I was trying to do. I wanted a library that decoupled it's processing logic from the view that would be rendered for the pagination markup.
One where you could define an instance of the Pagination class, define it's
properties programmatically, and have the rendered markup use the object to
generate clean, semantic, valid markup.
What's included?
This library contains two files:
PaginationInstantiable class for pagination objects/settingsrender.inc.phpView which generates the pagination markup
The relevant public methods for a Pagination instance are:
parseReturns a string of the generated pagination markupsetCrumbsSets the pagination to exclude page numbers, and only output preview/next markup. The counter-method of this is thesetFullmethodsetCurrentSets the maximum number of 'crumbs' (eg. numerical page items) that are shownsetCrumbsSets the current page being viewedsetFullSets the pagination to include numbers (counter-method ofsetCrumbs)setKeySets the key of the_GETarray that contains, and ought to contain, paging information (eg. which page is being viewed)setNextSets the copy of the next anchorsetPreviousSets the copy of the previous anchorsetRPPSets the number of records per page (used for determining total number of pages)setTargetSets the leading path for anchorssetTotalSets the total number of records available for pagination
How do I use it?
Using the Pagination library requires only a few lines of PHP, which will take you to a point whereby you have a variable that contains valid markup for your pagination list items/links.
This markup will not, however, be styled.. at all. For this, you'll have to use
the provided light.css stylesheet. Moving it over to your webroot/static
folder and including it should do the trick.
Hopefully over time, I'll be able to add some more themes :)
Why did I abstract it out?
I'm starting to question the efficacy and information that this section can offer. Anyway.
I abstracted this library out in the way that I did, because I wanted the application and view logic to be independent of one another.
I've seen pagination libraries before where a PHP class will generate markup inside of a massively-escaped method/function. I found this to be pretty inefficient, and made reading the view logic really difficult. Not to mention how difficult it was to make changes to the markup that would be generated.
This is a library that can be used independently from any framework.
Yup. Next up, PHP-MySQL.
Update (11.October.2012)
I've updated the library in order for it to be compatible with Twitter Bootstrap. I've made the following changes:
- Changed the
currentclass toactive - Moved this class from the anchor level to the list-item level
- Moved the
disabledclass from the anchor level to the list-item level - Added two methods in PHP (
addClassesandsetClasses) to allow you to specify UI-classes for the top-level pagination div (eg. adding thepagination-centeredclass)
See http://twitter.github.com/bootstrap/components.html#pagination for more.