Standalone pagination module that is not tied to any frameworks
Install via composer as metarush/pagination
use MetaRush\Pagination\Builder;
// define minimum required vars
$currentPage = $_GET['page'] ? (int) $_GET['page'] : 1;
$path = '/demo.php?page=';
$totalItems = 50;
$itemsPerPage = 5;$p = (new Builder)
->setTotalItems($totalItems)
->setItemsPerPage($itemsPerPage)
->setCurrentPage($currentPage)
->setPath($path)
->build();Display:
echo $p->prevLink() . ' ' . $p->pageLinksUncut() . ' ' . $p->nextLink();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();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();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>';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;$sql = "SELECT * FROM your_table LIMIT $limit OFFSET $offset";$result = DB::table('your_table')
->offset($offset)
->limit($limit)
->get();$queryBuilder
->select('*')
->from('your_table')
->setFirstResult($offset)
->setMaxResults($limit);Check the demo in public/demo.php



