Skip to content

Instantly share code, notes, and snippets.

@ZiTAL
Last active August 29, 2015 14:16
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 ZiTAL/bf9cf425ee20f708e8b1 to your computer and use it in GitHub Desktop.
Save ZiTAL/bf9cf425ee20f708e8b1 to your computer and use it in GitHub Desktop.
Pagination
<?php
function getPagination($page = 1, $amount = 10)
{
/*
1 -> 10 | 0 -> 9
2 -> 10 | 10 => 19
3 -> 10 | 20 => 29
4 -> 10 | 30 => 39
*/
$page = intval($page);
$amount = intval($amount);
if($page==0)
$page = 1;
if($amount==0)
$amount = 10;
$start = ($page - 1) * $amount;
$end = $page * $amount - 1;
$result = array
(
'start' => $start,
'end' => $end
);
return $result;
}
print_r(getPagination());
print_r(getPagination(0, 1));
print_r(getPagination(2, 20));
print_r(getPagination('a', 'b'));
/*
ourput:
Array
(
[start] => 0
[end] => 9
)
Array
(
[start] => 0
[end] => 0
)
Array
(
[start] => 20
[end] => 39
)
Array
(
[start] => 0
[end] => 9
)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment