1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| <?php
###############
#### logic ####
###############
function getClosestInferiorOddInt($int) {
return ((int)$int%2 == 0)? (int)$int-1:(int)$int;
}
/**
* "Converts" an array into a summarized pagination
* @param $array associative array (page number (1..n) => value)
* @param $current_page current page index
* @param $number_permanent_pages number of pages displayed at the beginning AND the end
(ex: $array length = 25, $number_permanent_pages = 2, we'll have for sure 1 2 and 24 25)
* @param $max_pages_displayed max. number of pages displayed (if the number is even, it is actually max+1)
* @return $pages array of pages to be displayed
*/
function getPagination($array, $current_page, $number_permanent_pages=2, $max_pages_displayed=11) {
//pages to be displayed
$pages = array();
$length_input = count($array);
if($length_input <= $max_pages_displayed || 2*$number_permanent_pages >= $length_input) {
$pages = $array;#nothing to do
}
else {
//last page of the first permament pages (left side)
$minimum_interval = $number_permanent_pages;
//first page of the last permament pages (right side)
$maximum_interval = $length_input-$number_permanent_pages+1;
//$number_permanent_pages first values in the array
for($i=1;$i <=$minimum_interval;$i++) {
$pages[$i] = $array[$i];
}
$amplitude = (getClosestInferiorOddInt($max_pages_displayed)-2*$number_permanent_pages+1)/2-1;
//loop
for($index=$amplitude; $index>=-$amplitude; $index--) {
//if no overlap with permanent pages, then, we add the page to the array
if(($current_page-$index > $minimum_interval) && ($current_page-$index < $maximum_interval)) {
$pages[$current_page-$index] = $array[$current_page-$index];
}
}
//$number_permanent_pages last values in the array
for($i=$maximum_interval;$i<=$length_input;$i++) {
$pages[$i] = $array[$i];
}
}
return $pages;
}
##############
#### demo ####
##############
## change parameters here ##
$length_input_array = 25;
$current_page = isset($_GET['page'])?intval($_GET['page']):rand(1,$length_input_array);
$max_pages_displayed = 11;
$permanent_pages = 2;
//init input array
for($i=1;$i<=$length_input_array;$i++) {
$array[$i] = ($i-1)*5;
}
//pagination
$pages = getPagination($array,$current_page,$permanent_pages,$max_pages_displayed);//2 last parameters are optional
## display ##
echo '<strong>Input array</strong><br /> ';
echo 'Length: <strong>'.$length_input_array.'</strong><br />';
echo 'Contents: '; print_r($array); echo '<hr />';
echo 'You will see the <strong>'.$permanent_pages.'</strong> first and last page(s).<br />';
echo 'You will never see more than <strong>'.$max_pages_displayed.'</strong> displayed pages.<br/>';
echo 'Current page is: <strong>'.$current_page.'</strong>.<br />';
echo 'Pages: ';
$last_index = null;
foreach($pages as $page_index => $value_not_used_for_demo) {
$dots = '';
if($last_index != null && $last_index != $page_index-1) {
$dots = ' ...';
}
echo $dots.' <a href="?page='.$page_index.'">'.$page_index.'</a>';
$last_index = $page_index;
}
echo '<br /><br />Current value: <strong>'.$pages[$current_page].'</strong>'; |
Partager