2010-02-17 26 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