An open API service indexing awesome lists of open source software.

https://github.com/iranianpep/paginator

A powerful PHP pagination engine to take care of pagination hassles
https://github.com/iranianpep/paginator

page paginable pagination pagination-generator pagination-library paginator paging

Last synced: 5 months ago
JSON representation

A powerful PHP pagination engine to take care of pagination hassles

Awesome Lists containing this project

README

          

# Paginator
A powerful PHP pagination engine to take care of pagination hassles

[![Build Status](https://travis-ci.org/iranianpep/paginator.svg?branch=master)](https://travis-ci.org/iranianpep/paginator)
[![Maintainability](https://api.codeclimate.com/v1/badges/9f8b2dd15bf3a8f48103/maintainability)](https://codeclimate.com/github/iranianpep/paginator/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/9f8b2dd15bf3a8f48103/test_coverage)](https://codeclimate.com/github/iranianpep/paginator/test_coverage)
[![StyleCI](https://styleci.io/repos/149567054/shield?branch=master)](https://styleci.io/repos/149567054)
[![Issue Count](https://codeclimate.com/github/iranianpep/paginator/badges/issue_count.svg)](https://codeclimate.com/github/iranianpep/paginator)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/1438614849564c76ac97e4eefccee63d)](https://www.codacy.com/app/iranianpep/paginator?utm_source=github.com&utm_medium=referral&utm_content=iranianpep/paginator&utm_campaign=Badge_Grade)

## Server Requirements
- PHP >= 7.1

## Installation
- Using Composer get the latest version:
```
composer require paginator/paginator
```

## Example
The simplest way to use Paginator is as follow:
```
$totalItems = 3;
$perPage = 1;
$currentPage = 1;
$url = 'https://example.com';

$paginator = new Paginator($totalItems, $perPage, $currentPage, $url);

if ($paginator->hasPages() === true) {
if ($paginator->getPreviousPage()) {
$previousPageUrl = $paginator->getPreviousPageUrl();

echo "

  • Previous
  • ";
    }

    foreach ($paginator->getPages() as $page) {
    if (!$page instanceof Page) {
    continue;
    }

    $pageNumber = $page->getNumber();
    $pageUrl = $page->getUrl();
    $cssClass = $page->isCurrent() === true ? 'active' : '';

    echo "

  • {$pageNumber}
  • ";
    }

    if ($paginator->getNextPage()) {
    $nextPageUrl = $paginator->getNextPageUrl();

    echo "

  • Next
  • ";
    }
    }
    ```

    If you are using any MVC framework like Laravel instantiate an object from Paginator class in the controller and pass it to the view:

    #### Controller
    ```
    public function index(Request $request)
    {
    $totalProductsNumber = 10;
    $perPage = 10;
    $currentPageNumber = (int) $request->get('page') > 0 ? $request->get('page') : 1;
    $url = $request->fullUrl();

    $paginator = new Paginator($totalProductsNumber, $perPage, $currentPageNumber, $url);

    // you should use the offset in your database query to get a slice of data
    $offset = $paginator->calculateDatabaseOffset($currentPageNumber);

    return view('view.index', [
    'paginator' => $paginator
    ]);
    }
    ```

    #### View (Laravel Blade)
    ```
    @if ($paginator->hasPages())


      @if ($paginator->isOnFirstPage() === true)
    • «

    • @else
    • «

    • @endif

      @php
      $hiddenRanges = $paginator->getHiddenRanges();
      @endphp

      @foreach ($paginator->getPages() as $page)
      {{-- "Three Dots" Separator --}}
      @if ((isset($hiddenRanges[0]) && $page->getNumber() === $hiddenRanges[0]['start']) ||
      (isset($hiddenRanges[1]) && $page->getNumber() === $hiddenRanges[1]['start']))

    • ...

    • @elseif((isset($hiddenRanges[0]) && $page->getNumber() > $hiddenRanges[0]['start'] && $page->getNumber() <= $hiddenRanges[0]['finish']) ||
      (isset($hiddenRanges[1]) && $page->getNumber() > $hiddenRanges[1]['start'] && $page->getNumber() <= $hiddenRanges[1]['finish']))
      @continue
      @else
      @if ($page->isCurrent())
    • {{ $page->getNumber() }}

    • @else
    • {{ $page->getNumber() }}

    • @endif
      @endif
      @endforeach

      @if ($paginator->isOnLastPage() === false)

    • »

    • @else
    • »

    • @endif

    @endif
    ```