Bonjour,
j'ai récupéré une class de pagination, mais elle a un comportement bizarre 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 ...?
J'obtiens ceci :
<< First
1 |2 |3 |Last >>
---------------------------------------------
Détail de ce qui est récupéré
---------------------------------------------
limit =>LIMIT 0,5
current =>1
previous =>1
next =>2
last =>34
info =>Page (1 of 34)
pages :
---0=>1
---1=>2
---2=>3
Et lorsque je clique sur la 8,10 ou plus page, j'obtiens :
<< First
22 |23 |24 |25 |26 |Last >>
---------------------------------------------
Détail de ce qui est récupéré
---------------------------------------------
limit =>LIMIT 115,5
current =>24
previous =>23
next =>25
last =>34
info =>Page (24 of 34)
pages :
---0=>22
---1=>23
---2=>24
---3=>25
---4=>26
Voici le code :
Code PHP :
<?php
<?
class pagination{
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 '<a href="pagination.php?p='.$v.'">'.$v.'</a> |';
}
}
echo '<a href="pagination.php?p='.$arr['last'].'">Last >></a>';
echo '<br />---------------------------------------------<br />';
echo 'Détail de ce qui est récupéré';
echo '---------------------------------------------<br />';
foreach ($arr as $k=>$v){
if (is_array($v)){
echo 'pages :<br />';
foreach($v as $key=>$value){
echo '---'.$key.'=>'.$value.'<br />';
}
}
else{
echo ''.$k.' =>'.$v.'<br />';
}
}
?>
?>
En gros cela affiche plus de lien que prévu (soit 3) à partir de la deuxième page et en plus quand je suis sur la dernière page, cela m'affiche des numéro de pages qui ne devrait pas exister.
Merci de m'aider car là, je coince.