metarush / pagination
Standalone pagination module that is not tied to any frameworks
Requires
- php: >=7.4
Requires (Dev)
- phpstan/phpstan: ^0.12
- phpunit/phpunit: ^9.5
- roave/security-advisories: dev-master
This package is auto-updated.
Last update: 2025-03-22 22:45:17 UTC
README
Standalone pagination module that is not tied to any frameworks
Install
Install via composer as metarush/pagination
Sample usage
use MetaRush\Pagination\Builder; // define minimum required vars $currentPage = $_GET['page'] ? (int) $_GET['page'] : 1; $path = '/demo.php?page='; $totalItems = 50; $itemsPerPage = 5;
Default config
$p = (new Builder) ->setTotalItems($totalItems) ->setItemsPerPage($itemsPerPage) ->setCurrentPage($currentPage) ->setPath($path) ->build();
Display:
echo $p->prevLink() . ' ' . $p->pageLinksUncut() . ' ' . $p->nextLink();
Display as dropdown
Remove ->build();
from previous example then append the ff.
->setPageLink('<option>{{page}}</option>') ->setActiveLink('<option selected="selected">{{page}}</option>') ->build();
Note: You need the ff. sample Javascript code for the dropdown to work
<script> document.getElementById('myDropdown').addEventListener('change', (event) => { location.href = `<?=$path?>${event.target.value}`; }); </script>
Display:
echo $p->prevLink() . ' <select id="myDropdown">' . $p->pageLinksUncut() . '</select> ' . $p->nextLink();
Auto-cut pagination
Remove ->setPageLink()
, ->setActiveLink()
, and ->build();
from previous example then append the ff.
->setPagesCutoff(5) // Estimated max number of page links to display (default: 7) ->build();
Display:
echo $p->prevLink() . ' ' . $p->pageLinksAutoCut() . ' ' . $p->nextLink();
Custom look
Use custom HTML/CSS or frameworks like Bootstrap/Materialize/etc.
Remove ->build();
from previous example then append the ff.
->setPageLink('<li class="page-item"><a class="page-link" href="{{path}}{{page}}">{{page}}</a></li>') ->setActiveLink('<li class="page-item active"><span class="page-link">{{page}}</span></li>') ->setDisabledPrevLink('<li class="page-item disabled"><span class="page-link">Prev</span></li>') ->setDisabledNextLink('<li class="page-item disabled"><span class="page-link">Next</span></li>') ->setEllipsis('<li class="page-item"><span class="page-link">...</span></li>') ->setPrevLink('<li class="page-item"><a class="page-link" href="{{path}}{{page}}">Prev</a></li>') ->setNextLink('<li class="page-item"><a class="page-link" href="{{path}}{{page}}">Next</a></li>') ->build();
Display:
echo '<ul class="pagination">' . $p->prevLink() . ' ' . $p->pageLinksAutoCut() . ' ' . $p->nextLink().' </ul>';
Querying data from database
Code for querying of data from a database is intentionally not included in this module. This is best implemented in userland. Below are examples on how to do this:
First, determine $limit
and $offset
$limit = $itemsPerPage; $offset = ($itemsPerPage * $currentPage) - $itemsPerPage;
Raw SQL
$sql = "SELECT * FROM your_table LIMIT $limit OFFSET $offset";
Laravel query builder
$result = DB::table('your_table') ->offset($offset) ->limit($limit) ->get();
Doctrine DBAL query builder
$queryBuilder ->select('*') ->from('your_table') ->setFirstResult($offset) ->setMaxResults($limit);
Working demo
Check the demo in public/demo.php