2017 © Pedro Peláez
 

library paginator

A lightweight PHP paginator, for generating pagination controls in the style of Stack Overflow and Flickr. The 'first' and 'last' page links are shown inline as page numbers, and excess page numbers are replaced by ellipses.

image

jasongrimes/paginator

A lightweight PHP paginator, for generating pagination controls in the style of Stack Overflow and Flickr. The 'first' and 'last' page links are shown inline as page numbers, and excess page numbers are replaced by ellipses.

  • Wednesday, July 11, 2018
  • by jasongrimes
  • Repository
  • 23 Watchers
  • 206 Stars
  • 134,028 Installations
  • PHP
  • 18 Dependents
  • 0 Suggesters
  • 68 Forks
  • 13 Open issues
  • 13 Versions
  • 8 % Grown

The README.md

PHP Paginator

Build Status Total Downloads Latest Stable Version Latest Unstable Version License, (*1)

A lightweight PHP paginator, for generating pagination controls in the style of Stack Overflow and Flickr. The "first" and "last" page links are shown inline as page numbers, and excess page numbers are replaced by ellipses., (*2)

Screenshots

These examples show how the paginator handles overflow when there are a lot of pages. They're rendered using the sample templates provided in the examples directory, which depend on Twitter Bootstrap. You can easily use your own custom HTML to render the pagination control instead., (*3)

Default template:, (*4)

br/ br/ br/, (*5)

Small template (useful for mobile interfaces):, (*6)

br/ br/ br/, (*7)

The small template renders the page number as a select list to save space:, (*8)

, (*9)

Installation

Install with composer:, (*10)

composer require "jasongrimes/paginator:~1.0"

Basic usage

Here's a quick example using the defaults:, (*11)

<?php

require '../vendor/autoload.php';

use JasonGrimes\Paginator;

$totalItems = 1000;
$itemsPerPage = 50;
$currentPage = 8;
$urlPattern = '/foo/page/(:num)';

$paginator = new Paginator($totalItems, $itemsPerPage, $currentPage, $urlPattern);

?>
<html>
  <head>
    <!-- The default, built-in template supports the Twitter Bootstrap pagination styles. -->
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
  </head>
  <body>

    <?php 
      // Example of rendering the pagination control with the built-in template.
      // See below for information about using other templates or custom rendering.

      echo $paginator; 
    ?>

  </body>
</html>

This will output the following:, (*12)

, (*13)

<ul class="pagination">
  <li><a href="/foo/page/7">&laquo; Previous</a></li>
  <li><a href="/foo/page/1">1</a></li>
  <li class="disabled"><span>...</span></li>
  <li><a href="/foo/page/5">5</a></li>
  <li><a href="/foo/page/6">6</a></li>
  <li><a href="/foo/page/7">7</a></li>
  <li class="active"><a href="/foo/page/8">8</a></li>
  <li><a href="/foo/page/9">9</a></li>
  <li><a href="/foo/page/10">10</a></li>
  <li><a href="/foo/page/11">11</a></li>
  <li><a href="/foo/page/12">12</a></li>
  <li class="disabled"><span>...</span></li>
  <li><a href="/foo/page/20">20</a></li>
  <li><a href="/foo/page/9">Next &raquo;</a></li>
</ul>

To render it with one of the other example templates, just make sure the variable is named $paginator and then include the template file:, (*14)

$paginator = new Paginator($totalItems, $itemsPerPage, $currentPage, $urlPattern);

include '../vendor/jasongrimes/paginator/examples/pagerSmall.phtml';

br/, (*15)

If the example templates don't suit you, you can iterate over the paginated data to render your own pagination control., (*16)

Rendering a custom pagination control

Use $paginator->getPages(), $paginator->getNextUrl(), and $paginator->getPrevUrl() to render a pagination control with your own HTML. For example:, (*17)

<ul class="pagination">
    <?php if ($paginator->getPrevUrl()): ?>
        <li><a href="<?php echo $paginator->getPrevUrl(); ?>">&laquo; Previous</a></li>
    <?php endif; ?>

    <?php foreach ($paginator->getPages() as $page): ?>
        <?php if ($page['url']): ?>
            <li <?php echo $page['isCurrent'] ? 'class="active"' : ''; ?>>
                <a href="<?php echo $page['url']; ?>"><?php echo $page['num']; ?></a>
            </li>
        <?php else: ?>
            <li class="disabled"><span><?php echo $page['num']; ?></span></li>
        <?php endif; ?>
    <?php endforeach; ?>

    <?php if ($paginator->getNextUrl()): ?>
        <li><a href="<?php echo $paginator->getNextUrl(); ?>">Next &raquo;</a></li>
    <?php endif; ?>
</ul>

<p>
    <?php echo $paginator->getTotalItems(); ?> found.

    Showing 
    <?php echo $paginator->getCurrentPageFirstItem(); ?> 
    - 
    <?php echo $paginator->getCurrentPageLastItem(); ?>.
</p>

See the examples directory for more sample templates., (*18)

Pages data structure

$paginator->getPages();

getPages() returns a data structure like the following:, (*19)

array ( 
    array ('num' => 1,     'url' => '/foo/page/1',  'isCurrent' => false),
    array ('num' => '...', 'url' => NULL,           'isCurrent' => false),
    array ('num' => 5,     'url' => '/foo/page/5',  'isCurrent' => false),
    array ('num' => 6,     'url' => '/foo/page/6',  'isCurrent' => false),
    array ('num' => 7,     'url' => '/foo/page/7',  'isCurrent' => false),
    array ('num' => 8,     'url' => '/foo/page/8',  'isCurrent' => true),
    array ('num' => 9,     'url' => '/foo/page/9',  'isCurrent' => false),
    array ('num' => 10,    'url' => '/foo/page/10', 'isCurrent' => false),
    array ('num' => 11,    'url' => '/foo/page/11', 'isCurrent' => false),
    array ('num' => 12,    'url' => '/foo/page/12', 'isCurrent' => false),
    array ('num' => '...', 'url' => NULL,           'isCurrent' => false),
    array ('num' => 20,    'url' => '/foo/page/20', 'isCurrent' => false),
)

Customizing the number of pages shown

By default, no more than 10 pages are shown, including the first and last page, with the overflow replaced by ellipses. To change the default number of pages:, (*20)

$paginator->setMaxPagesToShow(5);

The Versions

11/07 2018

dev-master

9999999-dev http://github.com/jasongrimes/php-paginator

A lightweight PHP paginator, for generating pagination controls in the style of Stack Overflow and Flickr. The 'first' and 'last' page links are shown inline as page numbers, and excess page numbers are replaced by ellipses.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Jason Grimes

pagination paginator pager

11/07 2018

1.0.3

1.0.3.0 http://github.com/jasongrimes/php-paginator

A lightweight PHP paginator, for generating pagination controls in the style of Stack Overflow and Flickr. The 'first' and 'last' page links are shown inline as page numbers, and excess page numbers are replaced by ellipses.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Jason Grimes

pagination paginator pager

15/10 2015

1.0.2

1.0.2.0 http://github.com/jasongrimes/php-paginator

A lightweight PHP paginator, for generating pagination controls in the style of Stack Overflow and Flickr. The 'first' and 'last' page links are shown inline as page numbers, and excess page numbers are replaced by ellipses.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Jason Grimes

pagination paginator pager

13/10 2014

1.0.1

1.0.1.0 http://github.com/jasongrimes/php-paginator

A lightweight PHP paginator, for generating pagination controls in the style of Stack Overflow and Flickr. The 'first' and 'last' page links are shown inline as page numbers, and excess page numbers are replaced by ellipses.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Jason Grimes

pagination paginator pager

11/10 2014

0.5.1

0.5.1.0 http://github.com/jasongrimes/php-paginator

A lightweight PHP paginator, for generating pagination controls in the style of Stack Overflow and Flickr. The 'first' and 'last' page links are shown inline as page numbers, and excess page numbers are replaced by ellipses.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Jason Grimes

pagination paginator pager

11/10 2014

1.0

1.0.0.0 http://github.com/jasongrimes/php-paginator

A lightweight PHP paginator, for generating pagination controls in the style of Stack Overflow and Flickr. The 'first' and 'last' page links are shown inline as page numbers, and excess page numbers are replaced by ellipses.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Jason Grimes

pagination paginator pager

01/10 2014

0.5

0.5.0.0 http://github.com/jasongrimes/php-paginator

A lightweight PHP paginator, for generating pagination controls in the style of Stack Overflow and Flickr. The 'first' and 'last' page links are shown inline as page numbers, and excess page numbers are replaced by ellipses.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Jason Grimes

pagination paginator pager

30/09 2014

0.4.2

0.4.2.0 http://github.com/jasongrimes/php-paginator

A lightweight PHP paginator, for generating pagination controls in the style of Stack Overflow and Flickr. The 'first' and 'last' page links are shown inline as page numbers, and excess page numbers are replaced by ellipses.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Jason Grimes

pagination paginator pager

30/09 2014

0.4.1

0.4.1.0 http://github.com/jasongrimes/php-paginator

A lightweight PHP paginator, for generating pagination controls in the style of Stack Overflow and Flickr. The 'first' and 'last' page links are shown inline as page numbers, and excess page numbers are replaced by ellipses.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Jason Grimes

pagination paginator pager

30/09 2014

0.4

0.4.0.0 http://github.com/jasongrimes/php-paginator

A lightweight PHP paginator, for generating pagination controls in the style of Stack Overflow and Flickr. The 'first' and 'last' page links are shown inline as page numbers, and excess page numbers are replaced by ellipses.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Jason Grimes

pagination paginator pager

28/09 2014

0.3

0.3.0.0 http://github.com/jasongrimes/php-paginator

A lightweight PHP paginator, for generating pagination controls like Stack Overflow or Flickr.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Jason Grimes

pagination paginator pager

28/09 2014

0.2

0.2.0.0 http://github.com/jasongrimes/php-paginator

A lightweight PHP paginator, for generating pagination controls like Stack Overflow or Flickr.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Jason Grimes

pagination paginator pager

28/09 2014

0.1

0.1.0.0 http://github.com/jasongrimes/php-paginator

A lightweight PHP paginator, for generating pagination controls like Stack Overflow or Flickr.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Jason Grimes

pagination paginator pager