Pagination module for PHPixie
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


PHPixie Pagination library

Build Status Test Coverage Code Climate HHVM Status

Author Source Code Software License Total Downloads

This is the base package for PHPixie Pagination that is split into several components.

// Initializing
$paginate = new \PHPixie\Paginate();

The base library cn only paginate arrays, there is an ORM extension available too, which we will look at further on. First let's see a usage example:

// Array with a 100 items
$data = range(1, 100);

// Initialize the pager
// with 15 items per page
$pager = $paginate->arrayPager($data, 15);

// Set current page

// A shorter way to do this:
$pager = $paginate->arrayPager($data, 15)

// Get some data
$pager->currentPage(); // 3
$pager->pageSize();    // 15
$pager->itemCount();   // 100
$pager->pageCount();   // 7 

// Get current items

// Check if page exists:
$pager->pageExists(1); // true

// Check if a page exists
// relative to the current one
$this->pageOffsetExists(-1); // true

// Get page number by relative offset
// Will return null if the page is missing
// In this case 4, since 3 is the current one

// Some shorthands,
// also return null if page missing
$this->next(); // 4
$this->previous(); // 2

A more interesting feature is getting pages adjacent to the current one, which is useful for rendering the pager.

// 15 items, split into 6 pages
$data = range(1, 18);
$pager = $paginate->arrayPager($data, 3);


$pager->getAdjacent(3); // array(2, 3, 4);

$pager->getAdjacent(2); // array(1, 2);

$pager->getAdjacent(4); // array(3, 4, 5, 6);


First we need to build the ORM pagination library:

$paginate = new \PHPixie\Paginate();
$paginateOrm = new \PHPixie\PaginateORM($paginate);

ORM pagination supports relationship preloading:

$pager = $paginateOrm->queryPager($query, 15);

// Or with the relationships specified
$pager = $paginateOrm->queryPager($query, 15, array('items'));

It will also consider the limit and offset you specified for the query. This means if you limit the items in the query before creating the pager, only those items will be paged:

$pager = $paginateOrm->queryPager($query, 15);

// Only those 100 items
// are in the pager
$pager->itemCount(); // 100