Hãy thử điều này,
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;
}