Page 1 of 1

Searching with MySql, almost got it

Posted: Tue Feb 05, 2008 5:59 pm
by invisibled
Ok so i have a site where you can search business's that sign up. I have 7 different fields to search by. Right now this is my query that runs when you submit the form.

Code: Select all

 
// Build SQL Query  
$query = "SELECT * FROM ida_business WHERE
            industry LIKE '%$tindustry%' OR
            region LIKE '%$tregion%' OR
            range LIKE '%$trange%' OR
            flow LIKE '%$flow%' OR
            abs LIKE '%$abs%' OR
            sale LIKE '%$tsale%' OR
            price LIKE '%$tprice%' 
         ORDER BY region ASC";
$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
 
I want it to only filter fields that the user selects from. And if they select more that one field it combines them and filters acordingly. I have the website online but dont want to post the link publicly, if you wish to see the site to get a better idea just say so and i will PM you the link.

This query isn't right but its the closest i could get it to being right. This is my first search script so any help will be great! If you need me to post more code just ask.

Thanks

Re: Searching with MySql, almost got it

Posted: Tue Feb 05, 2008 8:12 pm
by Christopher
Create an array of the field names from the form and loop thorough it to build the SQL.

Re: Searching with MySql, almost got it

Posted: Tue Feb 05, 2008 11:36 pm
by invisibled
so like, If field is not empty, display the variable in the query?

Re: Searching with MySql, almost got it

Posted: Tue Feb 05, 2008 11:46 pm
by Kieran Huggins
exactly

Re: Searching with MySql, almost got it

Posted: Fri Feb 15, 2008 2:22 pm
by invisibled
hmmmm i'm having alot of trouble with getting this going. could you show me what you mean? I get the concept, but I just can't quite figure out how to write it.