【问题标题】:php pagination display 5pages rangephp分页显示5页范围
【发布时间】:2013-09-06 17:29:35
【问题描述】:

我使用this tutorial 进行分页。

一切正常,但我想更改页码的显示。

教程代码生成页面链接如下。

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

我要更改的只是显示 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] > >>

我认为这部分需要更改。我试图搜索其他文章,但我找不到任何文章。什么是改变的好逻辑?谢谢。

// 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

添加:

最后一页也是 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]

我正在尝试将 5 个页码作为组放入数组中,以与 in_array 一起使用。还没有成功。

添加:

不知何故,我让它工作了......但是当前页面总是在中心,这不是我想要的...... :(

// 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

结果:

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]

我还是想喜欢..

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]

【问题讨论】:

    标签: php pagination


    【解决方案1】:
    // 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
    

    【讨论】:

    • 我运行了你的代码,但没有显示任何数字。只有 >> 显示。
    • 唯一显示的页码是5和10
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-29
    • 2014-06-17
    • 1970-01-01
    • 1970-01-01
    • 2016-04-03
    • 1970-01-01
    • 2014-11-02
    相关资源
    最近更新 更多