2017 © Pedro PelĂĄez
 

library pagerfanta

Pagination for PHP

image

pagerfanta/pagerfanta

Pagination for PHP

  • Monday, July 16, 2018
  • by pablodip
  • Repository
  • 48 Watchers
  • 1022 Stars
  • 6,256,495 Installations
  • PHP
  • 195 Dependents
  • 35 Suggesters
  • 150 Forks
  • 31 Open issues
  • 13 Versions
  • 9 % Grown

The README.md

NB This project is no longer maintained; you may like to use https://github.com/BabDev/Pagerfanta instead., (*1)

Pagerfanta

Build Status Scrutinizer Quality Score Code Coverage SensioLabsInsight Latest Stable Version Total Downloads, (*2)

This project is for PHP 7. If you need support for PHP < 7, use Release v1.1.0., (*3)

Usage

<?php

use Pagerfanta\Adapter\ArrayAdapter;
use Pagerfanta\Pagerfanta;

$adapter = new ArrayAdapter($array);
$pagerfanta = new Pagerfanta($adapter);

$pagerfanta->setMaxPerPage($maxPerPage); // 10 by default
$maxPerPage = $pagerfanta->getMaxPerPage();

$pagerfanta->setCurrentPage($currentPage); // 1 by default
$currentPage = $pagerfanta->getCurrentPage();

$nbResults = $pagerfanta->getNbResults();
$currentPageResults = $pagerfanta->getCurrentPageResults();

Some of the other methods available:, (*4)

$pagerfanta->getNbPages();
$pagerfanta->haveToPaginate(); // whether the number of results is higher than the max per page
$pagerfanta->hasPreviousPage();
$pagerfanta->getPreviousPage();
$pagerfanta->hasNextPage();
$pagerfanta->getNextPage();
$pagerfanta->getCurrentPageOffsetStart();
$pagerfanta->getCurrentPageOffsetEnd();

Changing the page based on user selection

If you're using the example route-generator function shown below, the page selected by the user will be available in the page GET (querystring) parameter., (*5)

You would then need to call setCurrentPage with the value of that parameter:, (*6)

if (isset($_GET["page"])) {
    $pagerfanta->setCurrentPage($_GET["page"]);
}

setMaxPerPage and setCurrentPage

The ->setMaxPerPage() and ->setCurrentPage() methods implement a fluent interface:, (*7)

<?php

$pagerfanta
    ->setMaxPerPage($maxPerPage)
    ->setCurrentPage($currentPage);

The ->setMaxPerPage() method throws an exception if the max per page is not valid:, (*8)

  • Pagerfanta\Exception\NotIntegerMaxPerPageException
  • Pagerfanta\Exception\LessThan1MaxPerPageException

Both extend from Pagerfanta\Exception\NotValidMaxPerPageException., (*9)

The ->setCurrentPage() method throws an exception if the page is not valid:, (*10)

  • Pagerfanta\Exception\NotIntegerCurrentPageException
  • Pagerfanta\Exception\LessThan1CurrentPageException
  • Pagerfanta\Exception\OutOfRangeCurrentPageException

All of them extend from Pagerfanta\Exception\NotValidCurrentPageException., (*11)

->setCurrentPage() throws an out ot range exception depending on the max per page, so if you are going to modify the max per page, you should do it before setting the current page., (*12)

(If you want to use Pagerfanta in a Symfony project, see https://github.com/whiteoctober/WhiteOctoberPagerfantaBundle.), (*13)

Adapters

The adapter's concept is very simple. An adapter just returns the number of results and an slice for a offset and length. This way you can adapt a pagerfanta to paginate any kind results simply by creating an adapter., (*14)

An adapter must implement the Pagerfanta\Adapter\AdapterInterface interface, which has these two methods:, (*15)

<?php

/**
 * Returns the number of results.
 *
 * @return integer The number of results.
 */
function getNbResults();

/**
 * Returns an slice of the results.
 *
 * @param integer $offset The offset.
 * @param integer $length The length.
 *
 * @return array|\Iterator|\IteratorAggregate The slice.
 */
function getSlice($offset, $length);

Pagerfanta comes with these adapters:, (*16)

ArrayAdapter

To paginate an array., (*17)

<?php

use Pagerfanta\Adapter\ArrayAdapter;

$adapter = new ArrayAdapter($array);

MongoAdapter

To paginate Mongo Cursors., (*18)

<?php

use Pagerfanta\Adapter\MongoAdapter;

$cursor = $collection->find();
$adapter = new MongoAdapter($cursor);

MandangoAdapter

To paginate Mandango Queries., (*19)

<?php

use Pagerfanta\Adapter\MandangoAdapter;

$query = $mandango->getRepository('Model\Article')->createQuery();
$adapter = new MandangoAdapter($query);

DoctrineDbalAdapter

To paginate DoctrineDbal query builders., (*20)

<?php

use Pagerfanta\Adapter\DoctrineDbalAdapter;
use Doctrine\DBAL\Query\QueryBuilder;

$queryBuilder = new QueryBuilder($conn);
$queryBuilder->select('p.*')->from('posts', 'p');

$countQueryBuilderModifier = function ($queryBuilder) {
    $queryBuilder->select('COUNT(DISTINCT p.id) AS total_results')
          ->setMaxResults(1);
};

$adapter = new DoctrineDbalAdapter($queryBuilder, $countQueryBuilderModifier);

DoctrineDbalSingleTableAdapter

To simplify the pagination of single table DoctrineDbal query builders., (*21)

This adapter only paginates single table query builders, without joins., (*22)

<?php

use Pagerfanta\Adapter\DoctrineDbalSingleTableAdapter;
use Doctrine\DBAL\Query\QueryBuilder;

$queryBuilder = new QueryBuilder($conn);
$queryBuilder->select('p.*')->from('posts', 'p');

$countField = 'p.id';

$adapter = new DoctrineDbalSingleTableAdapter($queryBuilder, $countField);

DoctrineORMAdapter

To paginate DoctrineORM query objects., (*23)

<?php

use Pagerfanta\Adapter\DoctrineORMAdapter;

$queryBuilder = $entityManager->createQueryBuilder()
    ->select('u')
    ->from('Model\Article', 'u');
$adapter = new DoctrineORMAdapter($queryBuilder);

DoctrineODMMongoDBAdapter

To paginate DoctrineODMMongoDB query builders., (*24)

<?php

use Pagerfanta\Adapter\DoctrineODMMongoDBAdapter;

$queryBuilder = $documentManager->createQueryBuilder('Model\Article');
$adapter = new DoctrineODMMongoDBAdapter($queryBuilder);

DoctrineODMPhpcrAdapter

To paginate Doctrine PHPCR-ODM query builders., (*25)

<?php

use Pagerfanta\Adapter\DoctrineODMPhpcrAdapter;

$queryBuilder = $documentManager->createQueryBuilder();
$queryBuilder->from('Model\Article');
$adapter = new DoctrineODMPhpcrAdapter($queryBuilder);

DoctrineCollectionAdapter

To paginate a Doctrine\Common\Collection\Collections interface you can use the DoctrineCollectionAdapter. It proxies to the count() and slice() methods on the Collections interface for pagination. This makes sense if you are using Doctrine ORMs Extra Lazy association features:, (*26)

<?php

use Pagerfanta\Adapter\DoctrineCollectionAdapter;

$user = $em->find("Pagerfanta\Tests\Adapter\DoctrineORM\User", 1);

$adapter = new DoctrineCollectionAdapter($user->getGroups());

DoctrineSelectableAdapter

To paginate a Doctrine\Common\Collection\Selectable interface you can use the DoctrineSelectableAdapter. It uses the matching() method on the Selectable interface for pagination. This is especially usefull when using the Doctrine Criteria object to filter a PersistentCollection:, (*27)

<?php

use Pagerfanta\Adapter\DoctrineSelectableAdapter;
use Doctrine\Common\Collections\Criteria;

$user = $em->find("Pagerfanta\Tests\Adapter\DoctrineORM\User", 1);
$comments = $user->getComments();
$criteria = Criteria::create()->andWhere(Criteria::expr()->in('id', array(1,2,3));

$adapter = new DoctrineSelectableAdapter($comments, $criteria);

Note that you should never use this adapter with a PersistentCollection which is not set to use the EXTRA_LAZY fetch mode., (*28)

Be careful when using the count() method, currently Doctrine2 needs to fetch all the records to count the number of elements., (*29)

ElasticaAdapter

To paginate an Elastica Query query:, (*30)

<?php

use Elastica\Index;
use Elastica\Query;
use Elastica\Query\Term;
use Pagerfanta\Adapter\ElasticaAdapter;

// Searchable can be any valid searchable Elastica object. For example a Type or Index
$searchable = new Index($elasticaClient, 'index_name');
// A Query can be any valid Elastica query (json, array, Query object)
$query = new Query::create(new Term(array(
    'name' => 'Fred'
));

$adapter = new ElasticaAdapter($searchable, $query);

Be careful when paginating a huge set of documents. By default, offset + limit can't exceed 10000. You can mitigate this by setting the $maxResults parameter when constructing the ElasticaAdapter. For more information, see: #213., (*31)

PropelAdapter

To paginate a propel 1 query:, (*32)

<?php

use Pagerfanta\Adapter\PropelAdapter;

$adapter = new PropelAdapter($query);

Propel2Adapter

To paginate a propel 2 query:, (*33)

<?php

use Pagerfanta\Adapter\Propel2Adapter;

$adapter = new Propel2Adapter($query);

SolariumAdapter

To paginate a solarium query:, (*34)

<?php

use Pagerfanta\Adapter\SolariumAdapter;

$query = $solarium->createSelect();
$query->setQuery('search term');

$adapter = new SolariumAdapter($solarium, $query);

FixedAdapter

Best used when you need to do a custom paging solution and don't want to implement a full adapter for a one-off use case., (*35)

It returns always the same data no matter what page you query:, (*36)

<?php

use Pagerfanta\Adapter\FixedAdapter;

$nbResults = 5;
$results = array(/* ... */);

$adapter = new FixedAdapter($nbResults, $results);

ConcatenationAdapter

Concatenates the results of other adapter instances into a single adapter. It keeps the order of sub adapters and the order of their results., (*37)

<?php

use Pagerfanta\Adapter\ConcatenationAdapter;

$superAdapter = new ConcatenationAdapter(array($adapter1, $adapter2 /* ... */));

Views

Views are to render pagerfantas, this way you can reuse your pagerfantas' HTML in several projects, share them and use another ones from another developer's., (*38)

The views implement the Pagerfanta\View\ViewInterface interface, which has two methods:, (*39)

<?php

/**
 * Renders a pagerfanta.
 *
 * The route generator is any callable to generate the routes receiving the page number
 * as first and unique argument.
 *
 * @param PagerfantaInterface $pagerfanta     A pagerfanta.
 * @param mixed               $routeGenerator A callable to generate the routes.
 * @param array               $options        An array of options (optional).
 */
function render(PagerfantaInterface $pagerfanta, $routeGenerator, array $options = array());

/**
 * Returns the canonical name.
 *
 * @return string The canonical name.
 */
function getName();

RouteGenerator example:, (*40)

<?php

$routeGenerator = function($page) {
    return '/path?page='.$page;
};

Pagerfanta comes with five views: The default one, three for Twitter Bootstrap, one for Semantic UI and a special optionable view., (*41)

DefaultView

This is the default view., (*42)

<?php

use Pagerfanta\View\DefaultView;

$view = new DefaultView();
$options = array('proximity' => 3);
$html = $view->render($pagerfanta, $routeGenerator, $options);

Options (default):, (*43)

  • proximity (3)
  • prev_message (Previous)
  • next_message (Next)
  • css_disabled_class (disabled)
  • css_dots_class (dots)
  • css_current_class (current)
  • dots_text (...)
  • container_template ()
  • page_template (%text%)
  • span_template (%text%)

CSS:, (*44)

.pagerfanta {
}

.pagerfanta a,
.pagerfanta span {
    display: inline-block;
    border: 1px solid blue;
    color: blue;
    margin-right: .2em;
    padding: .25em .35em;
}

.pagerfanta a {
    text-decoration: none;
}

.pagerfanta a:hover {
    background: #ccf;
}

.pagerfanta .dots {
    border-width: 0;
}

.pagerfanta .current {
    background: #ccf;
    font-weight: bold;
}

.pagerfanta .disabled {
    border-color: #ccf;
    color: #ccf;
}

COLORS:

.pagerfanta a,
.pagerfanta span {
    border-color: blue;
    color: blue;
}

.pagerfanta a:hover {
    background: #ccf;
}

.pagerfanta .current {
    background: #ccf;
}

.pagerfanta .disabled {
    border-color: #ccf;
    color: #cf;
}

TwitterBootstrapView, TwitterBootstrap3View and TwitterBootstrap4View

These views generate paginators designed for use with Twitter Bootstrap., (*45)

TwitterBootstrapView is for Bootstrap 2; TwitterBootstrap3View is for Bootstrap 3; TwitterBootstrap4View is for Bootstrap 4 (alpha)., (*46)

<?php

use Pagerfanta\View\TwitterBootstrapView;

$view = new TwitterBootstrapView();
$options = array('proximity' => 3);
$html = $view->render($pagerfanta, $routeGenerator, $options);

Options (default):, (*47)

  • proximity (3)
  • prev_message (← Previous)
  • prev_disabled_href ()
  • next_message (Next →)
  • next_disabled_href ()
  • dots_message (
)
  • dots_href ()
  • css_container_class (pagination)
  • css_prev_class (prev)
  • css_next_class (next)
  • css_disabled_class (disabled)
  • css_dots_class (disabled)
  • css_active_class (active)

SemanticUiView

This view generates a pagination for Semantic UI., (*48)

<?php

use Pagerfanta\View\SemanticUiView;

$view = new SemanticUiView();
$options = array('proximity' => 3);
$html = $view->render($pagerfanta, $routeGenerator, $options);

Options (default):, (*49)

  • proximity (3)
  • prev_message (← Previous)
  • prev_disabled_href ()
  • next_message (Next →)
  • next_disabled_href ()
  • dots_message (
)
  • dots_href ()
  • css_container_class (pagination)
  • css_item_class (item)
  • css_prev_class (prev)
  • css_next_class (next)
  • css_disabled_class (disabled)
  • css_dots_class (disabled)
  • css_active_class (active)

OptionableView

This view is to reuse options in different views., (*50)

<?php

use Pagerfanta\DefaultView;
use Pagerfanta\OptionableView;

$defaultView = new DefaultView();

// view and default options
$myView1 = new OptionableView($defaultView, array('proximity' => 3));

$myView2 = new OptionableView($defaultView, array('prev_message' => 'Anterior', 'next_message' => 'Siguiente'));

// using in a normal way
$pagerfantaHtml = $myView2->render($pagerfanta, $routeGenerator);

// overwriting default options
$pagerfantaHtml = $myView2->render($pagerfanta, $routeGenerator, array('next_message' => 'Siguiente!!'));

Contributing

We welcome contributions to this project, including pull requests and issues (and discussions on existing issues)., (*51)

If you'd like to contribute code but aren't sure what, the issues list is a good place to start. If you're a first-time code contributor, you may find Github's guide to forking projects helpful., (*52)

All contributors (whether contributing code, involved in issue discussions, or involved in any other way) must abide by our code of conduct., (*53)

Acknowledgements

Pagerfanta is inspired by Zend Paginator., (*54)

Thanks also to Pablo DĂ­ez (pablodip@gmail.com) for most of the work on the first versions of Pagerfanta., (*55)

Licence

Pagerfanta is licensed under the MIT License., (*56)

The Versions

23/09 2013

v1.0.1

1.0.1.0

Pagination for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

page paging pagination paginator

24/04 2013

v1.0.0

1.0.0.0

Pagination for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

page paging pagination paginator