next set of pages does not retain search variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
recipeDev
Forum Newbie
Posts: 6
Joined: Thu Jul 30, 2009 8:41 pm

next set of pages does not retain search variable

Post by recipeDev »

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

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">&nbsp;<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>"; 
?>
 
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: next set of pages does not retain search variable

Post by aceconcepts »

Have you thought about using POST so that all variables are retained in address bar?
recipeDev
Forum Newbie
Posts: 6
Joined: Thu Jul 30, 2009 8:41 pm

Re: next set of pages does not retain search variable

Post by recipeDev »

That doesn't work either, unless I'm to use it in a specific way?

Thanks.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: next set of pages does not retain search variable

Post by aceconcepts »

Using POST you are able to retain variables through many pages/reloads - as you know.

The variables which sounds like the important ones to you are page_number and results_per_page - so this can be used in a subsequent query in order to fetch the next set of records.

In order to tell the database which records to return you need to supply a range e.g. LIMIT 1, 20 etc...
Post Reply