steviez Posted September 12, 2008 Share Posted September 12, 2008 Hi, I have created some pagination code for my script and need some help to iron out a bug. When there are no more results to show i want the next button to not show but no matter what i do it always shows enableing the user to navigate through blank pages. Here is my code: <?php // Limit Results On Page $max_results = 9; $total_results = @mysql_result(mysql_query("SELECT COUNT(*) as Num FROM uploads WHERE album_id = '".$_GET['galleryID']."'"),0); $total_pages = ceil($total_results / $max_results); if(!isset($_GET['startrow']) or !is_numeric($_GET['startrow'])) { $startrow = 0; }else{ $startrow = (int)$_GET['startrow']; } if($total_results > $max_results) { $next_link = $site_url . '/albums.php?galleryID='.$_GET['galleryID'].'&startrow='.($startrow+$max_results).''; $next = "<a href=\"".$next_link."\" title=\"Next\" /><img src=\"".$site_url."/templates/css/images/next.gif\" alt=\"Next\" width=\"16\" height=\"16\" style=\"border:none;\" /></a>"; } if($startrow !== 0) { $previous_link = $site_url . '/albums.php?galleryID='.$_GET['galleryID'].'&startrow='.($startrow-$max_results).''; $prev = "<a href=\"".$previous_link."\" title=\"Previous\" /><img src=\"".$site_url."/templates/css/images/previous.gif\" alt=\"Previous\" width=\"16\" height=\"16\" style=\"border:none;\" /></a>"; } ?> Any help would be awsome Quote Link to comment Share on other sites More sharing options...
priti Posted September 12, 2008 Share Posted September 12, 2008 Hi Steve, <code> $total_results = @mysql_result(mysql_query("SELECT COUNT(*) as Num FROM uploads WHERE album_id = '".$_GET['galleryID']."'"),0); </code> It will result suppse 50 records <code>if($total_results > $max_results)</code> will always return 9 hence the if condition will always be true. you need to keep LIMIT in your query to fetch the current total records. <code> $total_results = @mysql_result(mysql_query("SELECT COUNT(*) as Num FROM uploads WHERE album_id = '".$_GET['galleryID']."'") LIMIT $start_limit,$max_results,0); on every page $start_limit will update. Regards </code> Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted September 12, 2008 Share Posted September 12, 2008 The only thing you need to check for the display of the next link is whether or not you're on the last page. E.g. if($current_page != $total_pages){ //display next link } If you're struggling, it might be worth reading through this tutorial: http://www.phpfreaks.com/tutorial/basic-pagination Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.