next set of pages does not retain search variable
Posted: Thu Aug 06, 2009 7:39 pm
Hi All,
I'm building a search feature on my recipe site, and the problem is the search variable is lost when clicking either 1, 2, 3, Next>> pages link after the initial first set of results (when results are larger than set limit result). So results show up fine for the first set of limit per page results, but after that, when you click any sequence of page numbers or next>> page, all records are returned and displayed from MySQL table, instead of being limited to the search word criteria.
My code is listed below, any help would be very much appreciated.
recipeDev
I'm building a search feature on my recipe site, and the problem is the search variable is lost when clicking either 1, 2, 3, Next>> pages link after the initial first set of results (when results are larger than set limit result). So results show up fine for the first set of limit per page results, but after that, when you click any sequence of page numbers or next>> page, all records are returned and displayed from MySQL table, instead of being limited to the search word criteria.
My code is listed below, any help would be very much appreciated.
recipeDev
Code: Select all
<?php
echo('
<!--- ****************** BEGIN Search Engine Builder Code for search engine ****************** --->
<FORM name=formSear action="search.php" method="GET">
<INPUT name=searWords size="25"> <INPUT name=Send type=submit value=Go>
</FORM>
')
?>
<?php
//get the search words
if (isset($_GET['searWords']))
{
$searchTerm = $_GET['searWords'];
}
// If current page number, use it
// if not, set one!
if(!isset($_GET['tblRTG_RECIPE_MAIN'])){
$page = 1;
} else {
$page = $_GET['tblRTG_RECIPE_MAIN'];
}
// Define the number of results per page
$max_results = 14;
// Figure out the limit for the query based
// on the current page number.
$from = (($page * $max_results) - $max_results);
//Retrieve all recipes found for search items found count
$resultCount ="SELECT RECIPE_ID, RECIPE_TITLE, CATEGORY, RECIPE_TYPE, SERVES, EST_TIME
FROM tblRTG_RECIPE_MAIN
WHERE RECIPE_TITLE LIKE '%$searchTerm%'";
//Retrieve all recipes by certain columns
$sql = mysql_query("SELECT RECIPE_ID, RECIPE_TITLE, CATEGORY, RECIPE_TYPE, SERVES, EST_TIME, RECIPE_PIC
FROM tblRTG_RECIPE_MAIN
WHERE RECIPE_TITLE LIKE '%$searchTerm%'
ORDER BY RECIPE_TITLE LIMIT $from, $max_results");
if (!$sql)
{
die('<p>Error retrieving results: ' . mysql_error() .
'</p>');
}
$resultCount = @mysql_query($resultCount);
$num_rowsCount= @mysql_num_rows($resultCount);
echo($num_rowsCount . ' result(s) found for <b>'. $searchTerm . ' </b>');
echo('<table border="0" width="80%">' . '<tr><td width="5%"></td><td width="37%" align="center"><p><b><u>' . "Recipe Title" . '</u></b></p></td><td width="14%" align="center"><p><b><u>' . "Category" .
'</u></b></p></td><td width="5%" align="center"><p><b><u>' . "Serves" .'</u></b></p></td><td width="10%" align="center"><p><b><u>' . "Est. Cook Time" . '</u></b></p></td></tr></table>');
// Display the results List
while ( $row = mysql_fetch_array($sql) )
{
$idt = $row['RECIPE_TITLE'];
echo('<hr width="755" align="left"/><table border="0" width="80%">'.
'<tr><td width="5%"><img src="'. $row['RECIPE_PIC'].'"' . ' width="55" height="50"></img></td><td width="37%" align="center"><p class="recipeInfo">' . '<a href="Recipes.php?id=' . $idt . '">' . $row['RECIPE_TITLE'] .
'</a></p></td><td width="14%" align="center"><p class="recipeInfo">' .$row['CATEGORY'] . '</p></td><td width="5%" align="center"><p class="recipeInfo">'
.$row['SERVES'] .'</p></td>' . '<td width="10%" align="center"><p class="recipeInfo">' .$row['EST_TIME'] .'</p></td></tr>' .
'</table>');
}
// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM tblRTG_RECIPE_MAIN WHERE RECIPE_TITLE LIKE '%$searchTerm%'"),0);
// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);
// Build Page Number Hyperlinks
echo "<center><p>Select a Page</p>";
// Build Previous Link
if($page > 1){
$prev = ($page - 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?tblRTG_RECIPE_MAIN=$prev\"><<Previous</a> ";
}
for($i = 1; $i <= $total_pages; $i++){
if(($page) == $i){
echo "$i ";
} else {
echo "<a href=\"".$_SERVER['PHP_SELF']."?tblRTG_RECIPE_MAIN=$i\">$i</a> ";
}
}
// Build Next Link
if($page < $total_pages){
$next = ($page + 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?tblRTG_RECIPE_MAIN=$next\">Next>></a>";
}
echo "</center>";
?>