2010-02-17 28 views
4

我正在尋找一個「高級」的php分頁腳本,每頁顯示10個mysql條目。在網上有許多「簡單的」腳本(甚至使用jQuery)是這樣的:http://www.9lessons.info/2009/09/pagination-with-jquery-mysql-and-php.html 下面是一個演示:http://demos.9lessons.info/pagination.php尋找先進的php/mysql分頁腳本

這些簡單的腳本有數百個條目時壞...所以我需要的是一個先進腳本 - 我需要的是這樣的:

當你第1頁上就應該是這樣的:

[1] 2 3 4 5 ... 45 

在第8頁:

1 ... 6 7 [8] 9 10 ... 45 

在第43頁:

1 ... 41 42 [43] 44 45 

等等...

許多論壇或博客(例如wordpress)正在使用這種技術。有人可以提供我的代碼嗎?必須有一個「最佳實踐代碼」,但我找不到它。 謝謝!

+0

關於SO的問題應顯示您嘗試過的代碼,而不僅僅是解決方案的一般要求。請參閱http://stackoverflow.com/about – Blazemonger 2014-02-24 15:47:17

回答

0

看一看this插件(jQuery的)

的分頁程序是不是你想要的東西有點不同,但它會做所有你所需要的。

5

嘗試了這一點,

function generatePagination($currentPage, $totalPages, $pageLinks = 5) 
{ 
    if ($totalPages <= 1) 
    { 
     return NULL; 
    } 

    $html = '<ul class="pagination">'; 

    $leeway = floor($pageLinks/2); 

    $firstPage = $currentPage - $leeway; 
    $lastPage = $currentPage + $leeway; 

    if ($firstPage < 1) 
    { 
     $lastPage += 1 - $firstPage; 
     $firstPage = 1; 
    } 
    if ($lastPage > $totalPages) 
    { 
     $firstPage -= $lastPage - $totalPages; 
     $lastPage = $totalPages; 
    } 
    if ($firstPage < 1) 
    { 
     $firstPage = 1; 
    } 

    if ($firstPage != 1) 
    { 
     $html .= '<li class="first"><a href="./?p=1" title="Page 1">1</a></li>'; 
     $html .= '<li class="page dots"><span>...</span></li>'; 
    } 

    for ($i = $firstPage; $i <= $lastPage; $i++) 
    { 
     if ($i == $currentPage) 
     { 
      $html .= '<li class="page current"><span>' . $i . '</span></li>'; 
     } 
     else 
     { 
      $html .= '<li class="page"><a href="./?p=' . $i . '" title="Page ' . $i . '">' . $i . '</a></li>'; 
     } 
    } 

    if ($lastPage != $totalPages) 
    { 
     $html .= '<li class="page dots"><span>...</span></li>'; 
     $html .= '<li class="last"><a href="./?p=' . $totalPages . '" title="Page ' . $totalPages . '">' . $totalPages . '</a></li>'; 
    } 

    $html .= '</ul>'; 

    return $html; 
} 
1

如果你真的有結果的網頁100S,你可能要考慮對數分頁。
有關詳細信息,請參閱this post