Forum Moderators: coopster

Message Too Old, No Replies

Limiting nr of page links in pagination

         

stever_nl

9:05 pm on Mar 15, 2005 (gmt 0)

10+ Year Member



Hey, would need some help with this trick,

As for now how my script works: it queries the DB and give the output with 10 per page, and makes a pagination on the bottom of the page like 1 2 3 4 ...

With some general search terms, i get more then 200 pages returned and it goes from 1 2 ... to 200 on the bottom of the page, this isn't really nice looking, so I was wondering if I can limit the pagination to 11 pagelinks at all times, so if there 200 pages and you're on page 25 you see on the bottom of the page:

< previous 20 21 22 23 24 25 26 27 28 29 30 next>

and if you are on page 29 it would be

< previous 24 25 26 27 28 29 30 31 32 33 34 next>

leaving the current page in the middle of the pagination with 5 previous pagelinks and 5 next pagelinks. And when clicking on previous or next shifting one page. Is this possible?

This is my current pagination:

if ($count > $lperpage) {

$linkshtml .= "<p>Page: ";
if ($page > 1) $linkshtml .= "<a href=\"search.$pext?query=".urlencode($query)."&page=".($page-1)."\">$pageprev</a> ";
for ($o=1; $o<=ceil($count/$lperpage); $o++) {
if ($page == $o) $linkshtml .= "$o ";
else $linkshtml .= "<a href=\"search.$pext?query=".urlencode($query)."&page=".($o)."\">$o</a> ";

}
if ($page < ceil($count/$lperpage)) $linkshtml .= "<a href=\"search.$pext?query=".urlencode($query)."&page=".($page+1)."\">$pagenext</a> ";
$linkshtml .= "</p>";
}

any help is appreciated!

cheers
Steve

ironik

12:44 am on Mar 16, 2005 (gmt 0)

10+ Year Member



You just need a couple of extra checks in there to output the right page numbers


// Store total pages
$totPages = ceil($count/$lperpage);

// First 11 pages, if current page is less than 5 pages in
if (($page - 5) < 1)
{
for ($o=1; $o<=11; $o++) {
if ($page == $o) $linkshtml .= "$o ";
else $linkshtml .= "<a href=\"search.$pext?query=".urlencode($query)."&page=".($o)."\">$o</a> ";
}
} else if (($page + 5) > $totPages)
{
// Output last 11 pages
for ($o= ($totPages - 11); $o<=$totPages; $o++) {
if ($page == $o) $linkshtml .= "$o ";
else $linkshtml .= "<a href=\"search.$pext?query=".urlencode($query)."&page=".($o)."\">$o</a> ";
}
} else {
// Output pages 5 below, and 5 above
for ($o= ($page - 5); $o<=($page + 5); $o++) {
if ($page == $o) $linkshtml .= "$o ";
else $linkshtml .= "<a href=\"search.$pext?query=".urlencode($query)."&page=".($o)."\">$o</a> ";
}
}

Untested, but hoping it helps anyway.

stever_nl

1:07 am on Mar 16, 2005 (gmt 0)

10+ Year Member



Ironik, again you helped me out!

Just needed to add the next and previous links, but managed that.

Big applause for Ironik!

Cheers
Steve

stever_nl

1:55 am on Mar 16, 2005 (gmt 0)

10+ Year Member



Found one thingie, if a search query has only 4 pages as a result, it still gives me a 1 to 11 pagination instead of the actual 1 to 4

So i need to build in a limit when the end has been reached.

I already know the nr of pages by

$totPages = ceil($count/$lperpage);

So i added this extra condition before all yours"


if ($totPages < 11) {
for ($o=1; $o<=ceil($count/$lperpage); $o++) {
if ($page == $o) $linkshtml .= "$o ";
else $linkshtml .= "<a href=\"search.$pext?query=".urlencode($query)."&page=".($o)."\">$o</a> ";
}
}
else {

worked!

thanks again!