搜索高级 php/mysql 分页脚本

2022-01-04 00:00:00 php mysql pagination paging

我正在寻找一个高级"的 php 分页脚本,每页显示 10 个 mysql 条目.在网络上有许多简单"脚本(即使使用 jQuery),如下所示: http://www.9lessons.info/2009/09/pagination-with-jquery-mysql-and-php.html这是一个演示:http://demos.9lessons.info/pagination.php>

当有数百个条目时,这些简单的脚本很糟糕......所以我需要一个高级脚本 - 我需要这样的东西:

当您在第 1 页时,它应该如下所示:

[1] 2 3 4 5 ... 45

在第 8 页:

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

第 43 页:

1 ... 41 42 [43] 44 45

等等...

许多论坛或博客(例如 wordpress)都在使用这种技术.有人可以给我提供代码吗?一定有最佳实践代码",但我找不到.谢谢!

解决方案

试试这个,

function generatePagination($currentPage, $totalPages, $pageLinks = 5){如果 ($totalPages <= 1){返回空;}$html = '
    ';$leeway = floor($pageLinks/2);$firstPage = $currentPage - $leeway;$lastPage = $currentPage + $leeway;如果 ($firstPage <1){$lastPage += 1 - $firstPage;$第一页 = 1;}如果 ($lastPage > $totalPages){$firstPage -= $lastPage - $totalPages;$lastPage = $totalPages;}如果 ($firstPage <1){$第一页 = 1;}如果 ($firstPage != 1){$html .= '<li class="first"><a href="./?p=1" title="第1页">1</a></li>';$html .= '<li class="页面点"><span>...</span></li>';}for ($i = $firstPage; $i <= $lastPage; $i++){如果 ($i == $currentPage){$html .= '<li class="当前页面"><span>'.$i .'</span></li>';}别的{$html .= '<li class="page"><a href="./?p='. $i .'" title="页面' . $i .'">'.$i .'</a></li>';}}如果 ($lastPage != $totalPages){$html .= '<li class="页面点"><span>...</span></li>';$html .= '<li class="last"><a href="./?p='. $totalPages .'" title="页面' . $totalPages . '">'.$totalPages .'</a></li>';}$html .= '</ul>';返回 $html;}

I'm searching for a "advanced" php Pagination script, that shows me 10 mysql entries per page. In the web there are many "simple" scripts (even with jQuery) like this: http://www.9lessons.info/2009/09/pagination-with-jquery-mysql-and-php.html Here is a demo: http://demos.9lessons.info/pagination.php

These simple scripts are bad when having hundreds of entries... So what I need is an advanced script - I need something like this:

When you are on Page 1 it should look like this:

[1] 2 3 4 5 ... 45

On Page 8:

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

On Page 43:

1 ... 41 42 [43] 44 45

and so on...

Many forums or blogs (e.g. wordpress) are using this technique. Can somebody please provide me with the code? There must be a "best practise code", but I can't find it. Thanks!

解决方案

Try this out,

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;
}

相关文章