簡體   English   中英

php分頁顯示5頁范圍

[英]php pagination display 5pages range

我使用本教程使用分頁。

一切都很好,但我想改變頁碼的顯示。

教程代碼生成頁面鏈接如下。

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