Searching using multiple fields

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
lifeasalounge
Forum Newbie
Posts: 1
Joined: Mon Feb 20, 2006 4:31 am

Searching using multiple fields

Post 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.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
BigJonMX
Forum Newbie
Posts: 15
Joined: Sun Nov 20, 2005 11:28 am
Location: CardboardBox, WindyCity

Post 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.
Post Reply