寻找高级的PHP/MySQL分页脚本

4
我正在寻找一个“高级”的php分页脚本,每页显示10个mysql条目。在网络上有很多“简单”的脚本(甚至带有jQuery),如下所示: http://www.9lessons.info/2009/09/pagination-with-jquery-mysql-and-php.html 这是一个演示: http://demos.9lessons.info/pagination.php 当有数百个条目时,这些简单的脚本表现不佳... 所以我需要一个高级脚本 - 我需要像这样的东西:
当您在第一页时,它应该像这样:
[1] 2 3 4 5 ... 45

第8页:
1 ... 6 7 [8] 9 10 ... 45

第43页:
1 ... 41 42 [43] 44 45

许多论坛或博客(例如wordpress)正在使用此技术。有人可以提供给我代码吗?必须有一个“最佳实践代码”,但我找不到它。 谢谢!

等等...


在SO上提出的问题应该展示你已经尝试过的代码,而不仅仅是一般性的解决方案请求。请参阅http://stackoverflow.com/about。 - Blazemonger
4个回答

6

请试试这个方法:

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

如果你真的有数百页的结果,你可能需要考虑对数分页。
详情请参见此帖子


0

看一下 this 插件(jquery)

分页器与您想要的有些不同,但它将完成您所需的所有功能。



网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接