2011-11-06 34 views
0

我有我已經貼在下面分頁腳本,問題是我有很多數據,所以我有一個巨大的網頁列表結束了,我想讓它顯示只有10頁一段時間,然後也許最後2頁這樣的:如何限制分頁腳本中所示的頁面

前1 2 3 4 5 6 7 8 9 ... 24 25下

有反正我可以改變代碼來做到這一點。下面是包含的分頁腳本,如果需要,我可以包含腳本的其他部分。

<?php 
//source unknown for logic of showPageNumbers() 
//modified by drale.com - 1-19-2010 
//added query_string reproduction and divs 
//added showNext() and showPrev() 

class Pagination 
{ 
    function getStartRow($page,$limit) 
    { 
     $startrow = $page * $limit - ($limit); 
     return $startrow; 
    } 

    function showPageNumbers($totalrows,$page,$limit) 
    { 
     $query_string = $this->queryString(); 
     $pagination_links = null; 

     /* 
     * PAGINATION SCRIPT 
     * seperates the list into pages 
     */  
     $numofpages = $totalrows/$limit; 
     /* We divide our total amount of rows (for example 102) by the limit (25). This 
      will yield 4.08, which we can round down to 4. In the next few lines, we'll 
      create 4 pages, and then check to see if we have extra rows remaining for 
      a 5th page. */ 

     for ($i = 1; $i <= $numofpages; $i++) { 
      /* This for loop will add 1 to $i at the end of each pass until $i 
       is greater than $numofpages (4.08). */  
      if ($i == $page) { 
       $pagination_links .= '<div class="page-link"><span>' . $i 
            . '</span></div> '; 
      } else { 
       $pagination_links .= '<div class="page-link"><a href="?page=' . $i 
        . '&' . $query_string . '">' . $i . '</a></div> '; 
      } 

      /* This if statement will not make the current page number available 
       in link form. It will, however, make all other pages available 
       in link form. */ 
     } // This ends the for loop 

     if (($totalrows % $limit) != 0) { 
     /* The above statement is the key to knowing if there are remainders, and it's 
     all because of the %. In PHP, C++, and other languages, the % is known as a 
     Modulus. It returns the remainder after dividing two numbers. If there is no 
     remainder, it returns zero. In our example, it will return 0.8 */ 

      if ($i == $page) { 
       $pagination_links .= '<div class="page-link"><span>' . $i 
            . '</span></div> '; 
      } else { 
       $pagination_links .= '<div class="page-link"><a href="?page=' . $i 
        . '&'.$query_string.'">'.$i.'</a></div> '; 
      } 
      /* This is the exact statement that turns pages into link 
       form that is used above */ 
     } // Ends the if statement 

     return $pagination_links; 
    } 

    //added by drale.com - 1-19-2010 
    function showNext($totalrows,$page,$limit,$text="next &raquo;") 
    { 
     $next_link = null; 
     $numofpages = $totalrows/$limit; 

     if ($page < $numofpages) { 
      $page++; 
      $next_link = '<div class="page-link"><a href="?page=' . $page 
         . '&'.$query_string.'">' . $text . '</a></div>'; 
     } 

     return $next_link; 
    } 

    function showPrev($totalrows,$page,$limit,$text="&laquo; prev") 
    { 
     $next_link = null; 
     $numofpages = $totalrows/$limit; 

     if ($page > 1) { 
      $page--; 
      $prev_link = '<div class="page-link"><a href="?page=' . $page 
       . '&' . $query_string . '">'.$text.'</a></div>'; 
     } 

     return $prev_link; 
    } 

    function queryString() 
    { 
     //matches up to 10 digits in page number 
     $query_string = eregi_replace("page=[0-9]{0,10}&","",$_SERVER['QUERY_STRING']); 
     return $query_string; 
    } 
} 
?> 
+1

恐怕還有比這更多的東西。例如,當你在25頁的第15頁時,你希望它的行爲如何,你是否應該在開始和結束時剪掉幾頁,或者你希望它如何工作? –

+0

對不起,我忘記了這一點,如果你在第15頁切斷一些在開始,所以它會喜歡:前7 8 9 10 11 12 13 14 15..24 25接下來。謝謝 – james

回答

1

未經測試,但應始終顯示頁面1 - 3和列表的最後3頁。否則,它只會顯示前一頁的3頁和接下來的三頁。 (當頁面的數量大於10)

$alwaysShowPages = array(1, 2, 3); 

// dynamically add last 3 pages 
for ($i = 3; $i >= 0; $i--) { 
    $alwaysShowPages[] = $numofpages - $i; 
} 

for ($i = 1; $i <= $numofpages; $i++) { 
    $showPageLink = true; 

    if ($numofpages > 10 && !in_array($i, $alwaysShowPages)) { 
     if (($i < $page && ($page - $i) > 3) 
      || ($i > $page && ($i - $page) > 3) 
     ) { 
      $showPageLink = false; 
     } 
    } 

    if ($showPageLink) { 
     if ($i == $page) { 
      $pagination_links .= '<div class="page-link"><span>'.$i.'</span></div> '; 
     } else { 
      $pagination_links .= '<div class="page-link"><a href="?page='.$i.'&'.$query_string.'">'.$i.'</a></div> '; 
     } 
    } 
} 
+0

這個解決方案在他的澄清評論中並不完全符合他的要求,但我相信這個解決方案從用戶的角度來看更有用。 –

+0

謝謝你們,這不完全是我想要的,但我認爲這樣會更好。有沒有辦法阻止它始終顯示前3頁,並在前後顯示3。感謝 – james

+1

假定$ alwaysShowPages之一是當前頁面,只要使用這樣的: 爲($ I = $當前第-3; $ I <= $當前第3; $ I ++){$ alwaysShowPages [] = $ I; } 當然,如果你需要擺脫重複: $ alwaysShowPages = array_unique($ alwaysShowPages); –

0

我有一個版本,這是否:

1 | | 3 | 4 | 5 | 6 ... 554 | 555 | 556

1 | 2 | 3 | 4 | | 6 ... 554 | 555 | 556

1 | 2 | 3 ... 278 | | 280 ... 554 | 555 | 556

1 | 2 | 3 ... 415 | | 417 ... 554 | 555 | 556

1 | 2 | 3 | 553 | | 555 | 556

的......實際上是走到半路當前頁面和鏈接的下一個(或最後)組的第1之間的聯繫。

我做了這麼一號6頁總是出現,如果當前頁面是小於5

但我所做的所有參數的動態,所以你可以改變變量一樣: $ end_links = 3; //設置每一端的鏈接數始終顯示 $ center_links; //鏈接,包括當前頁碼顯示「浮動」的中心

我花了時間做,這是非常可悲的。好吧。

只需使用數組了很多,並找出如何將正確的值添加到它。這真是簡單的數學和邏輯。

+0

對於那麼多頁面,「對數」頁面導航方案會更合適。 [見這裏](http://stackoverflow.com/q/7835752/999120)。 – Doin

+0

雖然你的答案似乎有太多的鏈接。我的方法肯定需要一些調整。但我試圖實現的結果模仿了一步一步猜測1到n之間數字的最有效過程。第一你猜n/2。那麼你猜n/2 + n/4或n/2 - n/4。然後n/2 +/- n/4 +/- n/8。等「蝴蝶」的方法。 –

+0

巴特爾,看看你的第一行:你如何得到p.200?對於「蝴蝶」方法,您至少需要5個鏈接:假設_n_頁和當前頁= _p_,則需要的鏈接是_1,p/2,p,(p + n)/ 2,n_。但這並不是用戶期望的分頁鏈接,並且可能會混淆大多數人,他們期望看到一系列可供選擇的鏈接。我的方法提供了範圍和「蝴蝶」選擇的能力(就像你所說的那樣)。 – Doin