Created
February 2, 2021 14:25
-
-
Save gharabaghi/c04b91d32315ea7efadb965af89dbe5f to your computer and use it in GitHub Desktop.
php- make page numbers
This file contains 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
public static function makePaginationPageNumbers($count, $perPage, $current, $offset) | |
{ | |
$start = 1; | |
$hasPrevious = false; | |
$hasFirst = false; | |
$hasNext = false; | |
$hasLast = false; | |
$end = intval(floor($count / $perPage) + ($count % $perPage > 0 ? 1 : 0)); | |
$pageNumbers = []; | |
for ($i = $current - 1; $i >= $start && $i >= $current - $offset; $i--) { | |
array_push($pageNumbers, $i); | |
} | |
for ($i = $current + 1; $i <= $end && $i <= $current + $offset; $i++) { | |
array_push($pageNumbers, $i); | |
} | |
array_push($pageNumbers, $current); | |
sort($pageNumbers); | |
if (array_search($start, $pageNumbers) === false) | |
$hasFirst = true; | |
if (array_search($end, $pageNumbers) === false) | |
$hasLast = true; | |
if ($current != $start) | |
$hasPrevious = true; | |
if ($current != $end) | |
$hasNext = true; | |
return [ | |
'HasFirst' => $hasFirst, | |
'HasLast' => $hasLast, | |
'HasPrevious' => $hasPrevious, | |
'HasNext' => $hasNext, | |
'PageNumbers' => $pageNumbers, | |
'First' => $start, | |
'Last' => $end, | |
'Current' => $current | |
]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment