...

View Full Version : Paginating



mokakid
05-18-2008, 03:00 PM
I don't have a clue about how to make a pagination system that it's 100% practical

i want to have something like this... in my example page 10 is the page where i am.

Go to page: First ... 6 7 8 9 10 11 12 13 14 ... Last

What i want? I want "First" 4 pages "the selected page" another 4 pages "Last"
and i want the code to automatically correct if the selected page is 1 so it will look something like this:

1 2 3 4 5 6 7 8 ... Last
or
First ... 5 6 7 8 9 10 11 12 13

i hope you understand what i want :thumbsup:

LAMPE
05-18-2008, 07:26 PM
you would just need to count the pages, and make multiple for loops.

for($a = 1; $a <= $num_of_pages; $a++){

}

Inigoesdr
05-18-2008, 08:12 PM
make multiple for loops.
Why would you need multiple loops?

mokakid, here is an example function you can use. Modify it however you need.

function paginate($current_page, $total_pages, $link)
{
$prev = $pages = $next = '';

if($current_page == 0)
$current_page = 1;
elseif($current_page > 1)
$prev = sprintf('<a href="&#37;s&page=%d">&laquo; Previous</a> ', $link, $current_page - 1);

for ($i = 1; $i <= $total_pages; $i++)
{
if($i == 1 && $current_page > 5)
{
$i = $current_page - 4;
$pages .= ' ... ';
}

if($current_page == $i)
$pages .= $i . ' ';
else
$pages .= sprintf('<a href="%s&page=%d">%d</a> ', $link, $i, $i);

if($i == $current_page + 4 && $total_pages > $current_page + 4)
{
$pages .= ' ... ';
break;
}
}

if($current_page < $total_pages)
$next = sprintf('<a href="%s&page=%d">Next &raquo;</a>', $link, $current_page + 1);

return $prev . $pages . $next;
}

echo 'Assuming 20 pages, and currently on page 10:<br />' . paginate(10, 20, '/search.php?q=stuff');
Outputs:

Assuming 20 pages, and currently on page 10:
&#171; Previous ... 6 7 8 9 10 11 12 13 14 ... Next &#187;

mokakid
05-19-2008, 05:51 PM
thx for the help :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum