Last active
August 29, 2015 14:16
-
-
Save ZiTAL/bf9cf425ee20f708e8b1 to your computer and use it in GitHub Desktop.
Pagination
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
<?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