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
- Host: GitHub
- URL: https://github.com/iranianpep/paginator
- Owner: iranianpep
- License: mit
- Created: 2018-09-20T07:09:36.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2020-02-09T21:47:32.000Z (over 5 years ago)
- Last Synced: 2025-04-01T04:51:12.396Z (7 months ago)
- Topics: page, paginable, pagination, pagination-generator, pagination-library, paginator, paging
- Language: PHP
- Homepage:
- Size: 60.5 KB
- Stars: 3
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Paginator
A powerful PHP pagination engine to take care of pagination hassles[](https://travis-ci.org/iranianpep/paginator)
[](https://codeclimate.com/github/iranianpep/paginator/maintainability)
[](https://codeclimate.com/github/iranianpep/paginator/test_coverage)
[](https://styleci.io/repos/149567054)
[](https://codeclimate.com/github/iranianpep/paginator)
[](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 "
}
foreach ($paginator->getPages() as $page) {
if (!$page instanceof Page) {
continue;
}
$pageNumber = $page->getNumber();
$pageUrl = $page->getUrl();
$cssClass = $page->isCurrent() === true ? 'active' : '';
echo "
}
if ($paginator->getNextPage()) {
$nextPageUrl = $paginator->getNextPageUrl();
echo "
}
}
```
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())
- «
- «
- ...
- {{ $page->getNumber() }}
- {{ $page->getNumber() }}
- »
- »
@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())
@else
@endif
@endif
@endforeach
@if ($paginator->isOnLastPage() === false)
@else
@endif
@endif
```