Next/Back links if statement

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
thoughtriot
Forum Commoner
Posts: 26
Joined: Thu Nov 07, 2002 9:32 pm

Next/Back links if statement

Post by thoughtriot »

Code: Select all

<?
include("guestbook/connect.php");
function dateParse($date)  
{  
     return $dateї2] . $dateї3] . "." . $dateї4] . $dateї5] . "." . $dateї0] . $dateї1]; 
}

if (!isset($page))  
     $page=0;  
  
if ($page < 0)  
     die("Illegal page value");

$offset = 0;
$number = 10;
  
$sql = "SELECT * FROM guestbook ORDER BY id DESC LIMIT " . ($number*$page) . "," . ($number*$page+10) . "";
$query = mysql_query($sql) or die("Cannot query the database.<br>" . mysql_error());
while($result = mysql_fetch_array($query)) {
$Name = stripslashes($resultї"Name"]);
$date = stripslashes($resultї"date"]);
$Email = stripslashes($resultї"Email"]);
$Comments = stripslashes($resultї"Comments"]);

echo "<table width=95% cellpadding=0 cellspacing=0><tr><td class=titletable><a href=mailto:$Email>$Name</a> on " . dateParse($date) . "</td></tr><tr><td>$Comments</td></tr></table><br>";
}
if ($page == 0)
print "<a href=guestbook.php?page=" . ($page+1) . ">Next</a>";

if ($page > 0)
print "<a href=guestbook.php?page=" . ($page-1) . ">Previous</a> | <a href=guestbook.php?page=" . ($page+1) . ">Next</a>";

?>
Okay, so everything works so far, and I did those if statements so that if the page is 0, there's no previous link. I would like to know if you could help be out with making it so when there's less than 10 posts on a page, there's no Next link. Thanks.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

you have to select count(*) which will give you the amount of available recordsets (before LIMITing them).
This divided by $number will give you the number of pages there are.

http://www.mysql.com/doc/en/Counting_rows.html
http://www.php.net/manual/en/function.ceil.php
Post Reply