Download the PHP package ashleydawson/simple-pagination without Composer
On this page you can find all versions of the php package ashleydawson/simple-pagination. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ashleydawson/simple-pagination
More information about ashleydawson/simple-pagination
Files in ashleydawson/simple-pagination
Package simple-pagination
Short Description Simple, lightweight and universal service that implements pagination on collections of things
License MIT
Informations about the package simple-pagination
Simple Pagination
Many thanks to BrowserStack for supporting open source projects like this one.
Simple pagination library implements a paging interface on collections of things. If you'd like to use the Simple Pagination within a Symfony 2 project then why not try my Simple Pagination Bundle.
Installation
You can install Simple Pagination via Composer. To do that, simply require
the
package in your composer.json
file like so:
Then run composer update
to install the package.
How Simple Pagination Works
I've tried to make Simple Pagination as easy to use and as flexible as possible. There are four main elements that describe the operation of Simple Pagination. These are:
- Paginator service
- Item total callback
- Slice callback
- Pagination model
The Paginator service performs the pagination algorithm, generating the page range and item collection slices. When it's done it will return a Pagination object filled with the item collection slice and metadata.
The two main operations the Paginator service will perform on your collection (or data set) are denoted by two callback methods passed to the Paginator service. The first one is the Item total callback. This callback is used to determine the total number of items in your collection (returned as an integer). The second one is the Slice callback. This callback actually slices your collection given an offset and length argument.
The idea behind using these callbacks is so that Simple Pagination is kept, well, simple! The real power comes with the flexibility. You can use Simple Pagination with just about any collection you want. From simple arrays to database lists to Doctrine collections to Solr result sets - we've got you covered! It really doesn't matter what we paginate - as long as it's a collection of things and you can count and slice it.
Basic Usage
Ok, lets go with the most basic example - paginating over an array.
There are lots of other pieces of meta data held within the pagination object. These can be used for building first, last previous and next buttons.
MySQL Example
Let's take the example above and use a MySQL result set instead of an array.
Note: The example above uses mysql_connect()
etc. as I tried to make it as simple as possible. In the real world please use PDO, Doctrine DBAL, etc.
It really doesn't matter what sort of collection you return from the Paginator::setSliceCallback() callback. It will always end up in Pagination::getItems().
Constructor Configuration
You can also configure the paginator with a configuration array passed to the constructor. For example:
Pagination as an Iterator
The Pagination object returned from the Paginator service implements \IteratorAggregate and \Countable so you can do things like this in your view:
Arbitrary Pagination Metadata
During both item total and slice callbacks you have the option of passing arbitrary metadata to the pagination object. This is an optional feature and is useful if you have a use-case where additional data is returned by these operations and you want to access it from the pagination object whilst listing the items. A good example of this is when using search engines such as ElasticSearch, you can pass back secondary information - like aggregations, etc. A generic example can be seen below:
Pre and Post Query Callbacks
Before and after the count and slice queries, you can set callbacks to fire. To set them, do the following:
This is handy if you want to perform some function before and after each query is made.
Pagination Object
The result of the Paginator::paginate() operation is to produce a Pagination model object, which carries the item collection for the current page plus the meta information for the collection, e.g. pages array, next page number, previous page number, etc. Please see below for a list of properties that the Pagination object has.
- items : mixed (Collection of items for the current page)
- pages : array (Array of page numbers in the current range)
- totalNumberOfPages : int (Total number of pages)
- currentPageNumber : int (Current page number)
- firstPageNumber : int (First page number)
- lastPageNumber : int (Last page number)
- previousPageNumber : int | null (Previous page number)
- nextPageNumber : int | null (Next page number)
- itemsPerPage : int (Number of items per page)
- totalNumberOfItems : int (Total number of items)
- firstPageNumberInRange : int (First page number in current range)
- lastPageNumberInRange : int (Last page number in current range)
A good example of using the Pagination object is to build a simple pagination navigation structure: