A powerful PHP pagination engine to take care of pagination hassles
- PHP >= 7.1
- Using Composer get the latest version:
composer require paginator/paginator
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 "<li><a href='{$previousPageUrl}'>Previous</a></li>";
}
foreach ($paginator->getPages() as $page) {
if (!$page instanceof Page) {
continue;
}
$pageNumber = $page->getNumber();
$pageUrl = $page->getUrl();
$cssClass = $page->isCurrent() === true ? 'active' : '';
echo "<li class='{$cssClass}'><a href='{$pageUrl}'>{$pageNumber}</a></li>";
}
if ($paginator->getNextPage()) {
$nextPageUrl = $paginator->getNextPageUrl();
echo "<li><a href='{$nextPageUrl}'>Next</a></li>";
}
}
If you are using any MVC framework like Laravel instantiate an object from Paginator class in the controller and pass it to the view:
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
]);
}
@if ($paginator->hasPages())
<ul class="pagination">
@if ($paginator->isOnFirstPage() === true)
<li class="page-item disabled"><span class="page-link">«</span></li>
@else
<li class="page-item"><a class="page-link" href="{{ $paginator->getPreviousPageUrl() }}" rel="prev" title="Previous">«</a></li>
@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']))
<li class="page-item disabled"><span class="page-link">...</span></li>
@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())
<li class="page-item active"><span class="page-link">{{ $page->getNumber() }}</span></li>
@else
<li class="page-item"><a class="page-link" href="{{ $page->getUrl() }}" title="Page">{{ $page->getNumber() }}</a></li>
@endif
@endif
@endforeach
@if ($paginator->isOnLastPage() === false)
<li class="page-item"><a class="page-link" href="{{ $paginator->getNextPageUrl() }}" rel="next" title="Next">»</a></li>
@else
<li class="page-item disabled"><span class="page-link">»</span></li>
@endif
</ul>
@endif