Topic: Paginate Function
I've asked about this before i am sure
if you have 9 pages and are on page 5 you get this
1 ... 3 4 5 6 7 ... 9
it should be
1 2 3 4 5 6 7 8 9 really
edit (thats not quite true its
so heres how it can be done (i know this is really messy
//
// Generate a string with numbered links (for multipage scripts)
//
function paginate($num_pages, $cur_page, $link_to)
{
$pages = array();
$link_to_all = false;
// If $cur_page == -1, we link to all pages (used in viewforum.php)
if ($cur_page == -1)
{
$cur_page = 1;
$link_to_all = true;
}
if ($num_pages <= 1)
$pages = array('<strong>1</strong>');
else
{
if ($cur_page > 3)
{
$pages[] = '<a href="'.$link_to.'&p=1">1</a>';
if ($cur_page > 5)
$pages[] = '…';
}
// Don't ask me how the following works. It just does, OK? :-)
for ($current = ($cur_page == 5) ? $cur_page - 3 : $cur_page - 2, $stop = ($cur_page + 4 == $num_pages) ? $cur_page + 4 : $cur_page + 3; $current < $stop; ++$current)
{
if ($current < 1 || $current > $num_pages)
continue;
else if ($current != $cur_page || $link_to_all)
$pages[] = '<a href="'.$link_to.'&p='.$current.'">'.$current.'</a>';
else
$pages[] = '<strong>'.$current.'</strong>';
}
if ($cur_page <= ($num_pages-3))
{
if ($cur_page != ($num_pages-3) && $cur_page != ($num_pages-4))
$pages[] = '…';
$pages[] = '<a href="'.$link_to.'&p='.$num_pages.'">'.$num_pages.'</a>';
}
}
return implode(' ', $pages);
}
The changes are the loop checks to see if its 4 pages from the start or 4 from the end and if so adds extra loops to the beginning or end, and ofcourse i also needed to change it to stop adding the ... if there was an extra loop.