Created
February 5, 2019 14:28
-
-
Save relliv/691c8920a74ba7f93835b558b5604748 to your computer and use it in GitHub Desktop.
PHP Pagination Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function pagination($total, $page, $limit, $query) | |
{ | |
$total = !is_numeric($total) ? 0 : (int) $total; | |
$page = !is_numeric($page) ? 1 : (int) $page; | |
$limit = !is_numeric($limit) ? 25 : (int) $limit; | |
$total = (int) $total; | |
// query limit start | |
$querystart = (($page === 0 ? 2 : $page) - 1) * $limit; | |
// last page page number | |
$last = ceil($total / $limit) ? ceil($total / $limit) : 1; | |
// first page number | |
$start = $page != $last && $page > $last ? ($page - 5) : 1; | |
// end of page before last page | |
$end = $page < $last ? $last - 1 : $last; | |
// select all for listing | |
$results = is_array($query) ? $query : $this->db->runSelect("{$query} LIMIT {$querystart}, {$limit}"); // example query base | |
return [ | |
'last' => $last, | |
'start' => $start, | |
'end' => $end, | |
'page' => $page, | |
'total_results' => $total, | |
'results' => $results, | |
]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment