PHPackages                             sivka/paginator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. sivka/paginator

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

sivka/paginator
===============

bootstrap-4 fork of 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.

2.1.0(5y ago)141.3k3MITPHPPHP &gt;=5.3.0CI failing

Since Feb 13Pushed 5y ago3 watchersCompare

[ Source](https://github.com/alexSivka/php-paginator)[ Packagist](https://packagist.org/packages/sivka/paginator)[ Docs](http://github.com/sivka/php-paginator)[ RSS](/packages/sivka-paginator/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (1)Versions (4)Used By (0)

PHP Paginator
=============

[](#php-paginator)

This is a clone of

Features added:

- Bootstrap 4,
- fixed first page url
- [size, position](#dimensions)

First page url is replaced by default with original url, but if the last param in constructor is false, it is generated as is. Example:

```
$paginator = new Paginator(
	$totalItems,
	$itemsPerPage,
	$currentPage,
	'/foo/(:num)');
```

The first page url will be '/foo/'

```
$paginator = new Paginator(
	$totalItems,
	$itemsPerPage,
	$currentPage,
	'/foo/(:num)',
	false);
```

The first page url will be '/foo/1'

Also you may use a callback for generate urls

```
$paginator = new Paginator(
  $totalItems,
  $itemsPerPage,
  $currentPage,
  function($pageNum){
    return '/page-' . $pageNum . '/';
  });
```

```
function getUrl($pageNum){
  return '/page-' . $pageNum . '/';
}

$paginator = new Paginator(
  $totalItems,
  $itemsPerPage,
  $currentPage,
  'getUrl');
```

```
class myClass {
  function getUrl($pageNum){
    return '/page-' . $pageNum . '/';
  }
}

$obj = new myClass;

$paginator = new Paginator(
  $totalItems,
  $itemsPerPage,
  $currentPage,
  [$obj, 'getUrl']);
```

---

dimensions
----------

[](#dimensions)

You can set size of paginator lg or sm

```
    $paginator = new Paginator(200, 10);
    $paginator->size('sm');
    echo $paginator;
```

And position of paginator center or right (default left)

```
    $paginator = new Paginator(200, 10);
    $paginator->position('right');
    echo $paginator;
```

position and size

```
    $paginator = new Paginator(200, 10);
    $paginator->dimensions($position, $size);
    echo $paginator;
```

Or your custom css class

```
    $paginator = new Paginator(200, 10);
    $paginator->size('my-custom-size-class');
    $paginator->position('my-custom-position-class');
    echo $paginator;
```

```
    $paginator = new Paginator(200, 10);
    $paginator->dimensions('my-custom-position-class', 'my-custom-size-class');
    echo $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.

Screenshots
-----------

[](#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](examples/) directory, which depend on Twitter Bootstrap. You can easily use your own custom HTML to render the pagination control instead.

Default template:

[![](examples/screenshot-default-first.png)](examples/screenshot-default-first.png)
[![](examples/screenshot-default-mid.png)](examples/screenshot-default-mid.png)
[![](examples/screenshot-default-last.png)](examples/screenshot-default-last.png)

Small template (useful for mobile interfaces):

[![](examples/screenshot-small-first.png)](examples/screenshot-small-first.png)
[![](examples/screenshot-small-mid.png)](examples/screenshot-small-mid.png)
[![](examples/screenshot-small-last.png)](examples/screenshot-small-last.png)

The small template renders the page number as a select list to save space:

[![](examples/screenshot-small-mid-open.png)](examples/screenshot-small-mid-open.png)

Installation
------------

[](#installation)

Install with composer:

```
composer require "sivka/paginator"

```

Basic usage
-----------

[](#basic-usage)

Here's a quick example using the defaults:

```

```

This will output the following:

[![](examples/screenshot-default-mid.png)](examples/screenshot-default-mid.png)

```

      &laquo; Previous
      1
      ...
      5
      6
      7
      8
      9
      10
      11
      12
      ...
      20
      Next &raquo;

```

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

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

    include '../vendor/sivka/paginator/examples/pagerSmall.phtml';
```

[![](examples/screenshot-small-mid.png)](examples/screenshot-small-mid.png)

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

Rendering a custom pagination control
-------------------------------------

[](#rendering-a-custom-pagination-control)

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

```

        getPrevUrl()): ?>
