简体   繁体   English

php分页显示5页范围

[英]php pagination display 5pages range

I use pagination using this tutorial . 我使用本教程使用分页。

Everything is ok, but I want to change display of page numbers. 一切都很好,但我想改变页码的显示。

The tutorial code generates pages link as below. 教程代码生成页面链接如下。

For page 1 : [1] 2 3 4 > >>
For page 6 : << < 3 4 5 [6] 7 8 9 > >> ( 3 pages range before and after).

What I want to change is showing only 5pages. 我想要改变的只是显示5页。

For page 1: [1] 2 3 4 5 > >>
For page 3: 1 2 [3] 4 5 > >>
For page 5: 1 2 3 4 [5] > >>
For page 6: << < [6] 7 8 9 10 > >>
For page 10: << < 6 7 8 9 [10] > >>

I think this part needs to be changed. 我认为这部分需要改变。 I tried to search other articles but I couldn't find any. 我试图搜索其他文章,但我找不到任何。 what is good logic to change? 什么是改变的好逻辑? Thanks. 谢谢。

// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
   // if it's a valid page number...
   if (($x > 0) && ($x <= $totalpages)) {
      // if we're on current page...
      if ($x == $currentpage) {
         // 'highlight' it but don't make a link
         echo " [<b>$x</b>] ";
      // if not current page...
      } else {
         // make it a link
         echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
      } // end else
   } // end if 
} // end for

Added: 添加:

Last page also in 5 pages format 最后一页也有5页格式

For page 1: [1] 2 3 4 5 > >>
For page 3: 1 2 [3] 4 5 > >>
For page 5: 1 2 3 4 [5] > >>
For page 6: << < [6] 7 8 9 10 > >>
For page 10: << < 6 7 8 9 [10] > >>
For last page 12: << < 8 9 10 11[12]

I am trying to put 5 pages numbers as group in array, to use with in_array. 我试图将5页数字作为数组中的组,与in_array一起使用。 No success yet. 还没有成功。

Added: 添加:

Somehow, I make it working... But current page is always in center which is not I wanted.. :( 不知何故,我让它工作......但当前页面总是在中心,这不是我想要.. :(

// range of num links to show
//$range = 3;
$range = 2;

....

// Added from here...    
if (($currentpage - $range) <= 1){
    $start_x = 1;
    $end_x = 5; 
}
else if ($currentpage >= ($totalpages - $range)){
    $start_x = $totalpages - 4;
    $end_x = $totalpages;
}
else {  
    $start_x = $currentpage - $range;
    $end_x = $currentpage + $range;
}
// Until here

// loop to show links to range of pages around current page
// for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
for ($x = $start_x; $x < ($end_x + 1); $x++) {
   // if it's a valid page number...
   if (($x > 0) && ($x <= $totalpages)) {
      // if we're on current page...
      if ($x == $currentpage) {
         // 'highlight' it but don't make a link
         echo " [<b>$x</b>] ";
      // if not current page...
      } else {
         // make it a link
         echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
      } // end else
   } // end if 
} // end for

Result: 结果:

For page 1: [1] 2 3 4 5 > >>
For page 3: 1 2 [3] 4 5 > >>
For page 5: << < 3 4 [5] 6 7 > >>
For page 6: << < 4 5 [6] 7 8 > >>
For page 10: << < 8 9 [10] 11 12 > >>
For last page 12: << < 8 9 10 11[12]

I still want like.. 我还是想要..

For page 1: [1] 2 3 4 5 > >>
For page 3: 1 2 [3] 4 5 > >>
For page 5: 1 2 3 4 [5] > >>
For page 6: << < [6] 7 8 9 10 > >>
For page 10: << < 6 7 8 9 [10] > >>
For last page 12: << < 8 9 10 11[12]
// loop to show links to range of pages around current page
for ($x = 1;  $x <= ($totalpages); $x++) {
   // if it's a valid page number...
   if (($x > 0) && ($x <= $totalpages)) {
       //limit for each 5 pages
      if ($x % 5 == 0) { 
      // if we're on current page...
         if ($x == $currentpage) {
            // 'highlight' it but don't make a link
            echo " [<b>$x</b>] ";
           // if not current page...
         } else {
            // make it a link
            echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
         } // end else
      }
   } // end if 
} // end for

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM