3 pièce(s) jointe(s)
Class pagination avec problème d'affichage
Bonjour,
j'ai récupéré une class de pagination, mais elle a un comportement bizzare que je n'arrive pas à rectifier.
J'indique bien qu'il doit m'en afficher que 3, ce qui marche pour les premières pages, mais après il augmente le nombre. Je pense que cela vient du limit mais pourquoi ...?
Je joint le script, le résultat en images.
Voici le script :
Code:
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 91 92 93 94 95 96 97 98 99
|
<?php
class pagination{
public function __construct() {
}
public function calculate_pages($total_rows, $rows_per_page, $page_num) {
$arr = array();
// calculate last page
$last_page =ceil($total_rows / $rows_per_page);
// make sure we are within limits
$page_num = (int) $page_num;
if ($page_num < 1) {
$page_num = 1;
}
elseif ($page_num > $last_page) {
$page_num = $last_page;
}
$upto =($page_num - 1)*$rows_per_page;
$arr['limit'] ='LIMIT '.$upto.',' .$rows_per_page;
$arr['current'] =$page_num;
if ($page_num === 1){
$arr['previous'] = $page_num;
}
else{
$arr['previous'] = $page_num - 1;
}
if ($page_num === $last_page){
$arr['next'] = $last_page;
}
else{
$arr['next'] = $page_num + 1;
}
$arr['last'] = $last_page;
$arr['info'] ='Page ('.$page_num.' of '.$last_page.')';
$arr['pages'] =$this->get_surrounding_pages($page_num, $last_page, $arr['next']);
return $arr;
}
function get_surrounding_pages($page_num, $last_page, $next){
$arr = array();
$show = 3; // how many boxes
// at first
if ($page_num === 1) {
// case of 1 page only
if ($next === $page_num){
return array(1);
}
for ($i = 0; $i < $show; $i++) {
if ($i === $last_page){
break;
}
array_push($arr, $i + 1);
}
return $arr;
}
// at last
if ($page_num === $last_page) {
$start = $last_page - $show;
if ($start < 1){
$start = 0;
}
for ($i = $start; $i < $last_page; $i++){
array_push($arr, $i + 1);
}
return $arr;
}
// at middle
$start = $page_num - $show;
if ($start < 1){
$start = 0;
}
for ($i = $start; $i < $page_num; $i++) {
array_push($arr, $i + 1);
}
for ($i = ($page_num + 1); $i < ($page_num + $show); $i++) {
if ($i === ($last_page + 1)){
break;
}
array_push($arr, $i);
}
return $arr;
}
}
$g_page=(isset($_GET['p']))? $_GET['p'] : 1;
$inst=new pagination();
$arr = $inst->calculate_pages(170, 5, $g_page);
echo '<a href="pagination.php?p=1"><< First</a> ';
foreach ($arr['pages'] as $k=>$v){
IF ((int)$arr['current']=== $v){
echo '<b>'.$v.'</b> |';
}
else{
//echo 'non';
echo '<a href="pagination.php?p='.$v.'">'.$v.'</a> |';
}
}
echo '<a href="pagination.php?p='.$arr['last'].'">Last >></a>';
?> |