I have this little pagination script and it works for the most part. but right now I am trying to get it to only show a “Previous” link if there is rows before and Next only if rows after the limit specified.
I got it to not show the previous link when no rows before but it won’t show next and previous on the next page only previous. which leads me to believe it is a if{}else{} failure on my part. but where? and how can I accomplish this if how I am doing it is wrong.
the code in question. (snippets of the important stuff)
[code=php]
$table_name = “guestbook”;
$table_2 = “guest_comments”;
$show = 6;
$start = (isset($_GET[‘start’]))?$_GET[‘start’]:0;
/////////
$sql2 = mysql_query(“SELECT COUNT(*) FROM $table_name”);
$howmany = mysql_fetch_row($sql2) or die(“Error Line 8.” . mysql_error());
global $guest_block;
$guest_block = “<table class=”gbook” width=”70%” align=”center” cellpadding=”5″ cellspacing=”0″>”;
$sql = “SELECT * FROM $table_name ORDER BY id DESC LIMIT $start, $show”;
$result = mysql_query($sql, $connection) or die (mysql_error());
$count = 0;
while($row = mysql_fetch_array($result)) {
$count++;
// get individual elements from the array
// all kinds of elements here…
}
////////
//after the while then I have this portion again to build the link.
////////
$total = $start + $show;
$next = $start + $show;
$prev = $start – $show;
////////
if(($total < $howmany[0]) && ($prev > 0))
{
$guest_block .= ”
<div align=”center”>
<a href=”$_SERVER[PHP_SELF]?start=$prev”>Previous $show</a>
| <a href=”$_SERVER[PHP_SELF]?start=$next”>Next $show</a>
</div>
<p>Showing Listings $start to $total of $howmany[0].</p>”;
}
if ($prev < 0)
{
$guest_block .= ”
<div align=”center”>
<a href=”$_SERVER[PHP_SELF]?start=$next”>Next $show</a>
</div>
<p>Showing Listings $start to $total of $howmany[0].</p>”;
}
else
{
$guest_block .= ”
<div align=”center”>
<a href=”$_SERVER[PHP_SELF]?start=$prev”>Previous $show</a>
</div>
<p>Showing Listings $start to $total of $howmany[0].</p>”;
}
}
Please any help will be appreciated. I can’t have it just always display a link because it is a bad query if there are no rows left or none previous to what is currently being shown…