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.
Searching using multiple fields
Moderator: General Moderators
-
lifeasalounge
- Forum Newbie
- Posts: 1
- Joined: Mon Feb 20, 2006 4:31 am
- CoderGoblin
- DevNet Resident
- Posts: 1425
- Joined: Tue Mar 16, 2004 10:03 am
- Location: Aachen, Germany
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.
$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.