Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Limiting pages in pagination links

Status
Not open for further replies.

bill1one

Programmer
Sep 29, 2004
93
US
I'm trying to make navigation easier for users. I would like to limit the page numbers shown to +/- 5 pages from current page, whereas currently it lists them all.

I've been trying to mess with the following section of my code, but to no luck, but it seems the answer is in this area of code:
Code:
$self = $_SERVER['PHP_SELF'];
$nav  = '';

for($page = 1; $page <= $maxPage; $page++)
{
   if ($page == $pageNum)
   {
      $nav .= " $page "; 
   }
   else
   {
      $nav .= " <a href=\"$self?page=$page&id=$id\">$page</a> ";
   }
}

if ($pageNum > 1)
{
   $page  = $pageNum - 1;
   $prev  = " <a href=\"$self?page=$page&id=$id\">[Prev]</a> ";

   $first = " <a href=\"$self?page=1&id=$id\">[First Page]</a> ";
}
else
{
   $prev  = '&nbsp;';  
   $first = '&nbsp;'; 
}

if ($pageNum < $maxPage)
{
   $page = $pageNum + 1;
   $next = " <a href=\"$self?page=$page&id=$id\">[Next]</a> ";

   $last = " <a href=\"$self?page=$maxPage&id=$id\">[Last Page]</a> ";
}
else
{
   $next = '&nbsp;'; 
   $last = '&nbsp;'; 
}

echo $first . $prev . $nav . $next . $last;

I can post more code if needed. I'm not a programmer so easy stuff like this is quite difficult for me. I'm not asking for answers either, I just would like to be pointed in the right direction. Thanks for all input.
 
why not use something like

Code:
for ($i = ($page-5); $i<($page + 5); $i++){
 if ($i < 0) continue; //ignore negative page numbers 
 if ($i > $maxPages) continue; //ignore non-existent page nums
 //do things with the active page number
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top