The first two replies have helped me a BIT.
I can now LIMIT the returned rows and implement the NEXT PREVIOUS links etc.
But there's a problem....
The search is prompted by a user entering a searchterm and a searchtype. The script in the results page has begins by testing to see if the searchterm and searchtype are there. If not, the user is pompted to return to the search options page.
In the script below (the HTML elements are very basic, just for testing purposes) things work up to the first stage. The search produces the first 20 found items. But, in a search that produces, say, 45 books in total, when you click next, instead of going to the next 20 rows, I either get the prompt to go back and enter search criteria or, with the following code, this message:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in c:\program files\apache group\apache\htdocs\testfiles\poresults2.php on line 53
even though this line is validated on the initial search...
here's the code:
<html>
<head>
<title>Poetry Results Page</title>
</head>
<body>
<h1>ACHUKA's Poetry Search Results</h1>
<?php
trim($searchterm);
$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);
$orderby = addslashes($orderby);
@ $db = mysql_pconnect("localhost", "root", "");
if (!$db)
{
echo "Error: Could not connect to database. Please try again later.";
}
$user="root";
$host="localhost";
$password="";
$database = "nonfiction";
$connection = mysql_connect($host,$user,$password)
or die ("couldn't connect to server");
$db = mysql_select_db($database,$connection)
or die ("Couldn't select database");
mysql_select_db("nonfiction");
$limit=20; // rows to return
$num_results=mysql_query("select * from poetry");
$num_rows=mysql_num_rows($num_results);
// next determine if offset has been passed to script, if not use 0
if (empty($offset)) {
$offset=1;
}
// get results
$query = "select Title, Name, Year, Publisher, Rating, Comment from poetry where ".$searchtype." like '%".$searchterm."%' order by ".$orderby." desc limit $offset,$limit";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
echo "<p>Number of books found: ".$num_results."</p>";
for ($i=0; $i <$num_results; $i++)
{
$row = mysql_fetch_array($result);
echo "<table width=300 border=1><tr><td>";
echo "<strong>".($i+1).". Title: ";
echo htmlspecialchars( stripslashes($row["Title"]));
echo "</td></tr><tr><td>";
echo "</strong><br>Author: ";
echo htmlspecialchars( stripslashes($row["Name"]));
echo "</td></tr><tr><td>";
echo "</strong><br>Year: ";
echo htmlspecialchars( stripslashes($row["Year"]));
echo "</td></tr><tr><td>";
echo "</strong><br>Publisher: ";
echo htmlspecialchars( stripslashes($row["Publisher"]));
echo "</td></tr><tr><td>";
echo "</strong><br>Rating: ";
echo htmlspecialchars( stripslashes($row["Rating"]));
echo "</td></tr><tr><td>";
echo "</td></tr></table>";
}
// next we need to do the links to other results
if ($offset==1) { // bypass PREV link if offset is 0
$prevoffset=$offset-20;
print "<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);
print "<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;
print "<a href=\"$PHP_SELF?offset=$newoffset\">NEXT</a><p>\n";
}
?>
</body>
</html>