dev-master
9999999-devAn extensible PHP pagination library
MIT
The Requires
- php >= 5.3.2
The Development Requires
by Anler Hernandez Peral
by Martin Häger
pagination paginator
Wallogit.com
2017 © Pedro Peláez
An extensible PHP pagination library
PHP-Paginator is a simple library. It's inspired by the Django's pagination and this [tutorial][2], following the best practices from the Symfony2 community., (*1)
First clone the repository with the dependencies:, (*2)
git clone git://github.com/minmb/PHP-Paginator.git cd PHP-Paginator git submodule init git submodule update
The goal of PHP-Paginator is to be as long as possible independent from the database engine and ORM used by the end user, in fact, you can use it even for paginate data fetched from other sources rather than a database. In order to have this flexibility PHP-Paginator can't be used directly in your project but with a minimal setup you will be ready to go., (*3)
The library is composed by four types of objects:, (*4)
PHP-Paginator has a Paginator, Page, SimpleLayout and PaginatedArray out of the box:, (*5)
<?php
require_once 'path/to/lib/PHP-Paginator/autoload.php';
use Paginator;
$paginatedItems = new Paginator\PaginatedArray(array(1, 2, 3, 4, 5));
$paginator = new Paginator\Paginator($paginatedItems);
$page = $paginator->getPage(1);
foreach($page as $item) {
printf('Item: %s', $item);
}
PaginatedArray is a class implementing the interface Paginator\PaginatedInterface. In order to create your own Paginated components, you need to implement the two methods of this interface which are:, (*6)
count() getSlice($length, $offset)
count() should return an integer representing the length of the dataset your working with getSlice($length, $offset) receives a length and an offset and should return a list/array of elements of your dataset., (*7)
Here's an example. In my custom project I have created a ProductsPaginatedQuery class that connects to my database:, (*8)
<?php
require_once 'path/to/lib/PHP-Paginator/autoload.php';
use Paginator;
class ProductsPaginatedQuery implements Paginator\PaginatedInterface
{
public function count()
{
return MyDBHandler::query("select count(*) from products");
}
public function get_slice($length, $offset)
{
return MyDBHandler::query("select * from products limit $length offset $offset");
}
}
$paginatedItems = new ProductsPaginatedQuery();
$paginator = new Paginator\Paginator($paginatedItems);
$page = $paginator->getPage(1);
foreach($page as $item) {
printf('Item: %s', $item);
}
As you see, you can make your paginated class as complicated and custom as you want, PHP-Paginator doesn't get in your way., (*9)
PHP-Paginator works with PHP 5.3.2 or later., (*10)
PHP-Paginator is licensed under the MIT license., (*11)
An extensible PHP pagination library
MIT
pagination paginator