Created
February 4, 2016 12:54
-
-
Save gskema/bed02042e59181927d24 to your computer and use it in GitHub Desktop.
Binary pagination generator
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 | |
/** | |
* Generates a pagination array | |
* | |
* @param int $total_items | |
* @param int $page_size | |
* @param int $current_page | |
* @return array $pagination | |
*/ | |
function pagination($total_items, $page_size, $current_page) | |
{ | |
// 1115 8 1 | |
// 1115 / 8 = 130 | |
/// 1 ... 33 ...65 65 66..78 80 81... 130 | |
$l = (int)ceil($total_items / $page_size); | |
$a = $current_page; | |
if ($l > 11 && $a < 6) { | |
$a = 1 + 5; | |
} | |
if ($l > 11 && ($l - $a) < 5) { | |
$a = $l - 5; | |
} | |
$left = $a - 1; | |
$right = $l - $a; | |
$p = array($a); | |
if ($left > 0 && $left < 6) { | |
$p = array_merge($p, range(1, $left)); | |
} | |
if ($left == 6) { | |
$p = array_merge($p, range($a - 3, $a - 1), array(1)); | |
} | |
if ($left > 6) { | |
array_push($p, 1, (int)(($a + 1)/2), $a - 1); | |
} | |
if ($right > 0 && $right < 6) { | |
$p = array_merge($p, range($a + 1, $l)); | |
} | |
if ($right == 6) { | |
$p = array_merge($p, range($a + 1, $a + 3), array($l)); | |
} | |
if ($right > 6) { | |
array_push($p, $a + 1, (int)(($l + $a)/2), $l); | |
} | |
sort($p); | |
return $p; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment