Skip to content

Instantly share code, notes, and snippets.

@gharabaghi
Created February 2, 2021 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gharabaghi/c04b91d32315ea7efadb965af89dbe5f to your computer and use it in GitHub Desktop.
Save gharabaghi/c04b91d32315ea7efadb965af89dbe5f to your computer and use it in GitHub Desktop.
php- make page numbers
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