API - lokothodida/php-arraypaginate GitHub Wiki

Index

Initialization

ArrayPaginate::__construct($array)
@param type optional description
$array Array array to paginate

Paginating the results

ArrayPaginate::paginate($options)

Returns Array that provides the pagination navigation HTML and the section of the results that correspond to the current page.

@param type optional description
$options Array array dictating the options

Paginate @options

@options type optional description
itemsPerPage Integer true maximum number of items per page; default 5
currentPage Integer true current page of results to show; default 1
url String true URL for pagination links, with a placeholder for the page number; default ?p=%page%
labels Array true mapping for labels in the pagination links; default: 'first' => '<<', 'prev' => '<', 'next' => '>', 'last' => '>>'
maxNavLinks Integer true maximum number of navigation links to show, excluding first/last etc; default 0 (turned off)
preserveKeys Boolean true whether or not to preserve the array keys in the results; default false

Paginate @return

@return type optional description
results Array segment of initial $items array that corresponds to the currentPage
navigation String HTML markup for pagination navigation links - consists of anchors for the links and a span for the current page
totalPages Integer total number of pages

Example

// assume our data is an array of comments from a blog post
$comments = array(/* */);

// instantiate
$ap = new ArrayPaginate($comments);

// 5 items per page, current page set by the url query and english labels
$results = $ap->paginate(array(
  'itemsPerPage' => 5,
  'currentPage'  => $_GET['page'],
  'url'          => 'http://yoursite.com/blog/post/1/comments/?page=%page%',
  'labels' => array(
    'first' => 'First',
    'prev' => 'Previous',
    'next' => 'Next',
    'last' => 'Last',
  ),
));

// output the navigation
echo $results['navigation'];

// state the total number of pages
echo '<p>Total pages : ' . $results['totalPages'] . '</p>';

// display the results for this page
foreach ($results['results'] as $result) {
  // ...
}