Hi,
I have made some progress with my guest book script thanks to the help provided on this website.
I return results from the database and page results. the only problem is that you can continue to click on next for an infinite amount of time, without stopping.
I have tried to calculate the total pages then stop the link once all the results have been shown but it just doesn't work!
I cant seem to see what's wrong!
here's the code 🙂
Thanks
Neil
<?php require("../inc/header.inc"); ?>
<table width="70%" align="center" >
<tr>
<td><div align="center">
<h1>Guestbook</h1>
</div></td>
</tr>
<tr>
<td><?php
//database values
$host = "localhost";
$user = "user";
$password = "password";
$database = "database";
$dbtable = "gbook";
$limit = "5"; // rows per page to return
// connect to database
$link = @ mysql_connect($host,$user,$password) or die ("Sorry <BR>\n Could not connect to the Guestbook. Please try later");
// next determine if offset has been passed to script, if not use 0
if (empty($offset))
{
$offset=1;
}
//query
$query = "SELECT * FROM $dbtable limit $offset, $limit";
// check to see if the query was successful
if (!mysql_db_query($database, $query, $link))
{
echo ("Sorry <br>\n Could not connect to the Guestbook!<BR>\n");
}
else
{
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
echo " There are <b>$num_results</b> messages in the guest book <br>\n<br>\n";
for ($i=0; $i <$num_results; $i++)
{
while ($row = mysql_fetch_array($result))
{
echo "<table width=345 border=1 cellpadding=0 cellspacing=0 bordercolor=E7E7E7>\n";
echo "<TR ALIGN=CENTER VALIGN=TOP>\n";
echo "<th><div align=left> Name:</div></th> <td> <div align=left>$row[name]</div></td>\n";
echo "</tr>\n";
echo "<TR ALIGN=CENTER VALIGN=TOP>\n";
echo "<th><div align=left>Email:</div></th> <td><div align=left>$row[email]</div></td>\n";
echo "</tr>\n";
echo "<TR ALIGN=CENTER VALIGN=TOP>\n";
echo "<th><div align=left> Date:</div></th> <td><div align=left>$row[pdate]</div></td>\n";
echo "</TR>\n";
echo "<TR ALIGN=CENTER VALIGN=TOP>\n";
echo "<th><div align=left>Country:</div></th> <td><div align=left>$row[country]</div></td>\n";
echo "</TR>\n";
echo "<th><div align=left>Message:</div></th> <td><div align=left>$row[comment]</div></td>\n";
echo "</tr>\n";
echo "</table><br>\n";
}
}
// next we need to do the links to other results
if ($offset >1)
{ // bypass PREV link if offset is 0
$prevoffset=$offset-5;
echo "<a href=\"$PHP_SELF?offset=$prevoffset\">PREV</a> \n";
}
// calculate number of pages needing links
$pages=intval($numrows/$limit);
// $pages now contains int of pages needed unless there is a remainder from division
if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}
for ($i=1;$i<=$pages;$i++) { // loop thru
$newoffset=$limit*($i-1);
echo "<a href=\"$PHP_SELF?offset=$newoffset\">$i</a> \n";
}
// check to see if last page
if (!(($offset/$limit)==$pages) && $pages!=1) {
// not last page so give NEXT link
$newoffset=$offset+$limit;
echo "<a href=\"$PHP_SELF?offset=$newoffset\">NEXT</a><p>\n";
}
}
// Close the database connection
if ($link = mysql_connect($host,$user,$password))
{
mysql_close ($link);
}
?></td>
</tr>
</table>
<?php require("../inc/footer.inc"); ?>