Jump to content

pagination help


steviez

Recommended Posts

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 :)

Link to comment
https://forums.phpfreaks.com/topic/123893-pagination-help/
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/123893-pagination-help/#findComment-639618
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/123893-pagination-help/#findComment-639619
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.