A lightweight PHP paginator without a database dependency, for generating pagination controls in the style of Yandex.
- The
first
andlast
page links are shown - The
current
andneighbours
page links are shown - Rest of links are replaced by ellipses
<< previous | next >>
|1| ... |5||6||7| ... |100|
$total = 100; // Total items
$perpage = 10; // Items per page
$current = 5; // Current page
$neighbours = 2; // Neighbours links beside current page
$y = new \dotzero\YPaginator($total, $perpage, $current);
$paginator = $y
->setNeighbours($neighbours)
->setUrlMask('#num#')
->setUrlTemplate('/foo/page/#num#')
->getPaginator();
print_r($paginator);
Output looks like:
[
"prev" => ["name" => 4,"url" => "/foo/page/4","current" => false], // Previous
"pages" => [
["name" => 1,"url" => "/foo/page/1","current" => false], // First
["name" => "...","url" => "/foo/page/2","current" => false],
["name" => 3,"url" => "/foo/page/3","current" => false], // Neighbour
["name" => 4,"url" => "/foo/page/4","current" => false], // Neighbour
["name" => 5,"url" => "/foo/page/5","current" => true], // Current
["name" => 6,"url" => "/foo/page/6","current" => false], // Neighbour
["name" => 7,"url" => "/foo/page/7","current" => false], // Neighbour
["name" => "...","url" => "/foo/page/8","current" => false],
["name" => 10,"url" => "/foo/page/10","current" => false] // Last
],
"next" => ["name" => 6,"url" => "/foo/page/6","current" => false] // Next
];
$ composer require dotzero/ypaginator
Clone the project using:
$ git clone https://github.com/dotzero/ypaginator-php
and include the source files with:
require_once("ypaginator-php/src/YPaginator.php");
First install the dependencies, and after you can run:
$ vendor/bin/phpunit
Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php