试试这个,
function generatePagination($currentPage, $totalPages, $pageLinks = 5)
{
if ($totalPages <= 1)
{
return NULL;
}
$html = '
- ';
$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 .= '
1';$html .= '
...';}
for ($i = $firstPage; $i <= $lastPage; $i++)
{
if ($i == $currentPage)
{
$html .= '
' . $i . '';}
else
{
$html .= '
' . $i . '';}
}
if ($lastPage != $totalPages)
{
$html .= '
...';$html .= '
' . $totalPages . '';}
$html .= '
';return $html;
}