php paging question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hward1973
Forum Newbie
Posts: 1
Joined: Thu Mar 22, 2007 9:57 am

php paging question

Post by hward1973 »

I am trying to get a paging page working and have been reading some tutorials on them but everyone I have found gives me

First | Prev | -Pages- | Next | Last

and I need

First | Prev 20 | -Pages- | Next 20 | Last

and only shows me 20 pages at a time this is the code for the one i am working with

Code: Select all

//how many pages we have when using paging? 
$maxPage = ceil($numrows/$rowsPerPage); 
if ($maxPage == null){
echo "
<body text='#FF0000' bgcolor='#000000'> No items to show at this time, Please check back.";
}
// print the link to access each page 
$self = $_SERVER['PHP_SELF']; 
$nav = ''; 
for($page = 1; $page <= $maxPage; $page++) 
{ 
    if ($page == $pageNum) 
    { 
        $nav .= " <font size='4' face='Arial' color='#008000'></font> ";   // no need to create a link to current page 
    } 
    else 
    { 
        $nav .= " <font size='4' face='Arial' color='#B68425'><a href=\"$self?page=$page&search=$search\">$page</a></font> "; 
    }         
} 
//////////////////////////////////////////////////////////////////////////


// creating previous and next link 
// plus the link to go straight to 
// the first and last page 

if ($pageNum > 1) 
{ 
    $page = $pageNum - 1; 
    $prev = " <font size='3' face='Arial' color='#008000'><a href=\"$self?page=$page&search=$search\">[Prev]</a></font> "; 
     
    $first = "<font size='3' face='Arial' color='#008000'><a href=\"$self?page=1&search=$search\">[First Page]</a></font> "; 
}  
else 
{ 
    $prev  = '&nbsp;'; // we're on page one, don't print previous link 
    $first = '&nbsp;'; // nor the first page link 
} 

if ($pageNum < $maxPage) 
{ 
    $page = $pageNum + 1; 
    $next = " <font size='3' face='Arial' color='#B68425'><a href=\"$self?page=$page&search=$search\">[Next]</a></font> "; 
     
    $last = " <font size='3' face='Arial' color='#B68425'><a href=\"$self?page=$maxPage&search=$search\">[Last Page]</a></font> "; 
}  
else 
{ 
    $next = '&nbsp;'; // we're on the last page, don't print next link 
    $last = '&nbsp;'; // nor the last page link 
} 
///////////////////////////////////////////////////////



// print the navigation link 
echo $first . $prev . $nav . $next . $last;
but it shows the $nav shows all the pages and i want to limit it to 20 at a time

any help would be great
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You are going to need to limit the loop to only 20 iterations when displaying the data.
Post Reply