Page 1 of 1
Searching using multiple fields
Posted: Mon Feb 20, 2006 4:39 am
by lifeasalounge
I am trying to create a php search. The problem i have is searching mutiple fields. I have six fields. When the search button is pressed these fields are saved as variables. A user does not have to search each field. They can search 1 or 2 or 3 or 4 or 5 or 6. I thought i would need to use if statements to find out which fields are empty and which are not. But is there an easier way to just grab the variables which are full? The way i am planning will have to use 36 if statements for each combination of field that is filled out. I hope someone can understand this.
Thanks.
Posted: Mon Feb 20, 2006 6:31 am
by CoderGoblin
I tend to add a separator and concatenate the the fields into one. (normally I use the | character) which is the field used for searching. This can all be done in one SQL select. Not sure if it is the best way, but it works.
Posted: Mon Feb 20, 2006 7:47 am
by BigJonMX
how about setting the default values for the fields, and then using the ones that are not fefault straight in your search.
$query="SELECT * FROM SearchTables WHERE a=a"; // the a=a gets all results, and gives us the WHERE word
if ($formfield1<>'defaultvalue'): $query=$query." AND column1=$formfield1;
if ($formfield2<>'defaultvalue'): $query=$query." AND column2=$formfield2;
$result=mysql_query($query, $connection)...etc...
Hope this helps,
Jon.