Search Form

 
Post new topic   Reply to topic    34SP.com Forum Index // Web Design
View previous topic :: View next topic  
Author Message
ryand
34SP Newbie
34SP Newbie


Joined: 13 Oct 2008
Posts: 7

PostPosted: Sat Nov 29, 2008 5:16 pm    Post subject: Search Form Reply with quote
Hi everyone
I have created a form which has 5 select fields in it. The user should be able to use any combination of these 5 fields i.e. they can select 1,2,3,4 or 5 possible search options.

I am using php so on the page these values will be sent to what is the best way to take the results and use them to get the right information from the database.

Is it a matter of writing code to say if field 1 is selected but not 2,3,4,5 then do this
else if field 1 and 2 are selected but not 3,4,5 etc..

Any help would be much appreciated!

Thanks
Back to top
View user's profile Send private message Visit poster's website
Nimco
Junior Member
Junior Member


Joined: 30 Sep 2002
Posts: 151
Location: York

PostPosted: Mon Dec 01, 2008 9:33 am    Post subject: Reply with quote
Could you not assign a search criteria to each option, and then append the criteria to a search string?

Code:
<?php

$query = "SELECT * FROM Table WHERE Status='Active'";

$criteria= array(
"1"=>"`Field1"`='true'",
"2"=>"`Field2"`='true'",
"3"=>"`Field3"`='true'",
"4"=>"`Field4"`='true'",
"5"=>"`Field5"`='true'"
);

for ($i=1;$i<=5;$i++) {
    if (isset($_POST['options'][$i]) && $_POST['options'][$i]=="true") {
        $query .= " AND {$criteria[$i]}";
    }
}

$result = mysql_query($query);

?>
Back to top
View user's profile Send private message
ryand
34SP Newbie
34SP Newbie


Joined: 13 Oct 2008
Posts: 7

PostPosted: Mon Dec 01, 2008 3:22 pm    Post subject: Reply with quote
Thanks for your help. Im new to php altogether so I was trying to understand that code but I don't think I can follow it. Do you mean that if the user selects something from any of the options that a 'status' is activated. Then if its active its added to the SQL code? If it helps, the code I have so far is:

Code:
<form action="results.php" method="post">
<table>
<tr>
<td>Breed</td>
<td ><select name="breed" id="breed">
          <option>Breed 1     </option>
  <option>Breed 2</option>
  <option>Breed 3</option>
</select></td>
</tr>
<tr>
<td>Price</td>
<td><select name="price" id="price">
          <option>Price 1</option>
  <option>Price 2</option>
  <option>Price 3</option>
</select></td>
</tr>
<tr>
<td>Location</td>
<td width="127"><select name="location" id="location">
          <option>Northern Ireland</option>
  <option>England</option>
  <option>Scotland</option>
  <option>Wales</option>
  <option>ROI</option>
  <option>Europe</option>
  <option>Worldwide</option>
</select></td>
</tr>
<tr>
<td>End Date</td>
<td><select name="date" id="date">
          <option>Today</option>
  <option>Within 1 Week</option>
  <option>Within 1 Month</option>
</select></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Submit" /></td>
</tr>
</table>
</form>
Back to top
View user's profile Send private message Visit poster's website
Post new topic   Reply to topic    34SP.com Forum Index // Web Design All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Powered by phpBB © 2001, 2002 phpBB Group