A lightweight PHP paginator, for generating pagination controls in the style of Stack Overflow and Flickr.

Overview

PHP Paginator

Build Status Total Downloads Latest Stable Version Latest Unstable Version License

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

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.

Default template:




Small template (useful for mobile interfaces):




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

Installation

Install with composer:

composer require "jasongrimes/paginator:~1.0"

Basic usage

Here's a quick example using the defaults:

<?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:

<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:

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

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


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

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

<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.

Pages data structure

$paginator->getPages();

getPages() returns a data structure like the following:

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:

$paginator->setMaxPagesToShow(5);
Issues
Releases(1.0.3)
Owner
Jason Grimes
Jason Grimes
A base API controller for Laravel that gives sorting, filtering, eager loading and pagination for your resources

Bruno Introduction A Laravel base controller class and a trait that will enable to add filtering, sorting, eager loading and pagination to your resour

Esben Petersen 157 Dec 10, 2021
In-place pagination without page refresh by using Backbone.js with Laravel PHP framework

Laravel Backbone - based in-place pagination demo Store application See demo at: http://demos.maxoffsky.com/ajax-pagination/ Tutorial at: http://maxof

Maksim Surguy 42 Jan 3, 2022
Bring Laravel 8's cursor pagination to Laravel 6, 7

Laravel Cursor Paginate for laravel 6,7 Installation You can install the package via composer: composer require vanthao03596/laravel-cursor-paginate U

Pham Thao 6 Jan 13, 2022
Cursor pagination for your Laravel API

Cursor Pagination for Laravel This package provides a cursor based pagination already integrated with Laravel's query builder and Eloquent ORM. It cal

Juan Pablo Barreto 70 Sep 25, 2021
A simple Laravel package for generating download links with options such as expire time, IP restrictions, etc.

Generate download links in your Laravel applications This package allows you to generate download links for files. Once installed you can do stuff lik

Arman Ahmadi 7 Nov 25, 2021
Avatar Generating Package for Laravel.

Avatar Generating Package for Laravel.

Tall 1 Oct 28, 2021
Library for generating random names (for table-top roleplaying games)

RPG-Name-Generator The RPG character name generator library is designed to create list of random names used for table-top role-playing games. This lib

Florent Genette 1 Nov 29, 2021
Run patches migration style in your Laravel applications.

This package generates patch files in the same fashion Laravel generates migrations. Each file is timestamped with an up and a down method and is asso

Anthony Rappa 30 Nov 1, 2021
Laravel style jquery validation plugin

Validator Laravel style jquery validation plugin Validator is a jQuery plugin that emulates the validation class found in the laravel framework. Usage

David Thingsaker 3 Feb 1, 2018
Let's base your Laravel project with PROS style

Purpose This library is for convenient methods that use to register code base for Laravel project Target We aimed to reduce complexity for real projec

Protean Studios Co., Ltd. 2 Jan 12, 2022
Livewire UI components with tailwind base style

WireUI ?? Documentation Wire UI is a library of components and resources to empower your Laravel and Livewire application development. Starting a new

WireUi 297 Jan 13, 2022
Viewi for Laravel: Build full-stack and completely reactive user interfaces with PHP.

[WIP] Laravel Viewi This is just a proof-of-concept. Don't use it in production! Viewi for Laravel: Build full-stack and completely reactive user inte

Protone Media 4 Jan 7, 2022
Laravel Livewire (TALL-stack) form generator with realtime validation, file uploads, array fields, blade form input components and more.

TALL-stack form generator Laravel Livewire, Tailwind forms with auto-generated views. Support Contributions Features This is not an admin panel genera

TinaH 547 Jan 14, 2022
Gallium is a TALL stack starter kit offering a robust set of options enabling you to get up and running in a snap.

Very short description of the package This is where your description should go. Try and limit it to a paragraph or two, and maybe throw in a mention o

null 1 Nov 20, 2021
A full-stack framework for Laravel that takes the pain out of building dynamic UIs.

A full-stack framework for Laravel that takes the pain out of building dynamic UIs.

Livewire 14.2k Jan 14, 2022
Livewire Notifier is a simple notifications system with zero dependencies above TALL-stack

Livewire Notifier is a simple notifications system with zero dependencies above TALL-stack (Tailwind CSS, Alpine.JS, Laravel and Livewire).

CodeSPB 17 Dec 25, 2021
Laravel Package to generate CRUD Files using TALL Stack

tall-crud-generator Laravel Package to generate CRUD Files using TALL Stack Requirements Make sure that Livewire is installed properly on your project

AscSoftwares 61 Dec 31, 2021
Real world Conduit App based on Laravel Livewire stack

Laravel Livewire codebase containing real world examples (CRUD, auth, advanced patterns, etc) that adheres to the RealWorld spec and API. Demo Github

Ricardo Sawir 16 Dec 30, 2021
The source code of the Full Stack Europe website.

Full Stack Europe This repository contains the source code for the https://fullstackeurope.com website. Requirements The following tools are required

Full Stack Europe 33 Oct 24, 2021