php分页显示5页范围 [英] php pagination display 5pages range

查看:62
本文介绍了php分页显示5页范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用本教程使用分页.

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

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).

我要更改的内容仅显示5页.

What I want to change is showing only 5pages.

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

已添加:

最后一页也是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一起使用.还没有成功.

I am trying to put 5 pages numbers as group in array, to use with in_array. No success yet.

已添加:

我以某种方式使其正常工作...但是当前页面始终位于中心位置,这是我不想要的..:(

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

结果:

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]

推荐答案

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

这篇关于php分页显示5页范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆