Passing search results a session variable

Associate
Joined
6 Mar 2009
Posts
495
Hi guys,

I have a simple search function in php which gets info from my database which works fine. This is a simple search which gets all data on specific products. I would then like to do an advanced search on the results from the first search. I was thinking of passing the first set of results as a session variable to another page and do more search from there. I have used the same method of passing variables which have worked before but it wont work for my search results.

Below is the code i have used to try and display my search results on a new page.
Code:
 <?php
   $_SESSION['result'];

echo "<table class='searchTable' border='1';  <tr>"; 
for($i = 0; $i < mysql_num_fields.$_SESSION['result']; $i++) {     
$field_info = mysql_fetch_field($_SESSION['result'], $i);     
echo "<th>{$field_info->name}</th>"; }   

// Print the data 

while($row = mysql_fetch_row($result)) {     
echo "<tr>";     
	foreach($row as $_column) {         
		echo "<td>{$_column}</td>";     }     
		echo "</tr>"; 
		}  
		
		echo "</table>"; 
 
 ?>

ps i have used the session_start on required pages. Also i dont think when echoing out into the table i have done it right either with the session variables.

Here is the code for the first search i have done.

Code:
$query = ("SELECT * FROM ($table) WHERE upper($field) LIKE '%" . $find . "%'");
$result = mysql_query($query);

echo "<table class='searchTable' border='1';  <tr>"; 
for($i = 0; $i < mysql_num_fields($result); $i++) {     
$field_info = mysql_fetch_field($result, $i);     
echo "<th>{$field_info->name}</th>"; }  

// Print the data 

while($row = mysql_fetch_row($result)) {     
echo "<tr>";     
	foreach($row as $_column) {         
		echo "<td>{$_column}</td>";     }     
		echo "</tr>"; 
		}  
		
		echo "</table>";

Can anyone help me with this please.

Thanks
 
Ok guys i have decided to go about this a different way. I have set up a few drop down boxes that will be used to define the search. It works fine if all search critera is set, but i would like to be able to leave a drop down menu blank and for the search to carry on the query with the information provided. As so far all fields has to have a item selected to be able to complete the search. Here is my code:

Code:
$query = ("SELECT * FROM ($table) WHERE upper($field) LIKE '%" . $find . "%' AND (Initials = '$Initials') AND (PassFail = '$PassFail')" );
$result = mysql_query($query);

echo "<table class='searchTable' border='1';  <tr>"; 
for($i = 0; $i < mysql_num_fields($result); $i++) {     
$field_info = mysql_fetch_field($result, $i);     
echo "<th>{$field_info->name}</th>"; }  

// Print the data 

while($row = mysql_fetch_row($result)) {     
echo "<tr>";     
	foreach($row as $_column) {         
		echo "<td>{$_column}</td>";     }     
		echo "</tr>"; 
		}  
		
		echo "</table>";

Has anyone any ideas??
 
Ok here is the search.php
Code:
<form name="search" method="post" action="search_submit.php">
 
 Product
 <Select NAME="table">
 <Option VALUE="limestone4mmtesting">Limestone 4mm</option>
 <Option VALUE="limestone2mmtesting">Limestone 2mm</option>
 <Option VALUE="limestone1mmtesting">Limestone 1mm</option>
 <Option VALUE="aglime">AG Lime</option>
 <Option VALUE="finewhiting">Limestone Fine Whiting</option>
 <Option VALUE="sandgrade2">Limestone Sand Grade 2</option>
 <Option VALUE="sandgrade1">Limestone Sand Grade 1</option>
 </Select>
 </br>
 </br>
 Search by Date: <input name="find" type="text" /> 
 <Select NAME="field">
 <option value="Date">Date</option>
 </Select>
 </br>
 </br>
 User
 <Select NAME="Initials">
 <Option VALUE="steven">Steven</option>
 <Option VALUE="test">Test</option>
 <Option>----------</option>
 </Select>
 </br>
 </br>
 Pass/Fail
 <Select NAME="PassFail">
 <Option VALUE="Pass">Pass</option>
 <Option VALUE="Fail">Fail</option>
 <Option>----------</option>
 </Select>  
	
 <input type="hidden" name="searching" value="yes" />
 <input type="submit" name="search" value="Search" />
 </form>

And here is search_submit.php

Code:
$query = ("SELECT * FROM ($table) WHERE upper($field) LIKE '%" . $find . "%' AND (Initials = '$Initials') AND (PassFail = '$PassFail')" );
$result = mysql_query($query);

echo "<table class='searchTable' border='1';  <tr>"; 
for($i = 0; $i < mysql_num_fields($result); $i++) {     
$field_info = mysql_fetch_field($result, $i);     
echo "<th>{$field_info->name}</th>"; }  

// Print the data 

while($row = mysql_fetch_row($result)) {     
echo "<tr>";     
	foreach($row as $_column) {         
		echo "<td>{$_column}</td>";     }     
		echo "</tr>"; 
		}  
		
		echo "</table>"; 
 
 //This counts the number or results - and if there wasn't any it gives them a little message explaining that 
 $anymatches=mysql_num_rows($result); 
 if ($anymatches == 0) 
 { 
 echo "Sorry, but we can not find an entry to match your query<br><br>"; 
 } 
 
 //And we remind them what they searched for 
 echo "<b>Searched For:</b> " .$find;

So far im only searching with these four categories. The field to enter a date can be left blank and still carry on with the search which i would like the other drop down menus to do. p.s Would it be hard to do a date range picker instead of typing in a date??

Thanks
 
The first problem I see is that you have no blank item in the first drop down so you can't 'not' select something.

I don't really understand what you are trying to do by looking at the code. You could construct a string to use as the mysql query using if statements to add each drop down to the query if a value has been selected.

You also don't seem to have copied all of the code as you are using $find in the query but $find is never set with $_POST.
 
I have tried setting the first drop down to ' ' (empty quotes) but didnt work so i took them out.

And i have set $find with $_POST its just at the top of the page and didnt want to post every bit of code.

I think the issue is in the Query - if a field is empty then dont search for it. But not sure how to do this!!
 
Last edited:
Use ifs to create a string?

$QSBuild ="";
if ($find !=null){
$QSBuild += "SELECT * FROM ($table) WHERE upper($field) LIKE '%" . $find . "%'"
};
if ($initials !=null){
$QSBuild +="AND (Initials = '$Initials')"
}
$query = ($QSBuild);



Something like that maybe? There is probably something you can actually write in the sql query but I'm not that good.
 
Hi Stryda09, thanks for the help. Although im not sure if the += is the correct way to add to the string. Done a little research and think i have to use the CONCAT method.
 
Hi Stryda09, thanks for the help. Although im not sure if the += is the correct way to add to the string. Done a little research and think i have to use the CONCAT method.

lol well I was making you do a bit more work than copy and paste ;) you can use "." to concatenate though.

PHP:
$QSBuild = $QSBuild."blah blah";
 
Last edited:
Back
Top Bottom