Searching for advanced php/mysql pagination script [closed]

删除回忆录丶 提交于 2019-11-28 11:39:31

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

If you really have 100s of pages of results, you might want to consider paginating logarithmically.
See this post for details.

Have a look at this plugin (jquery)

The paginator is a bit different than what you want but It will do all what you need.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!