Page 1 of 1

How to make a filtered search with mysql?

Posted: Sun Aug 05, 2007 2:18 am
by scheinarts
Hi,

I came up with the challenge of making a search form, such that users can select how they want to search:

By Model # (only numbers)
Just one keyword (ex: cellphone = will only return results containing the keyword cellphone)
Multiple keywords (ex: cell phone = will return results containing both the words cell and phone)

Then make these 3 search options as check boxes. I know how to code the sql queries, but I dont know how to code the php so that it selects which proper query based on which checkbox has been selected.

Maybe someone has an example for this, or can describe what method to use. Thanks in advance!

Posted: Sun Aug 05, 2007 12:31 pm
by Christopher
Check to see if each value has a value and concatenate additional conditions to the SQL string.

Posted: Sun Aug 05, 2007 3:01 pm
by scheinarts
you mean check to see if one of the check boxes has been selected?, but i dont get how to concatenate additional string... you mean conditionals inside the sql string?
Because I was thinking doing this... if(thisCheckBox){run this sql query}

Posted: Sun Aug 05, 2007 7:27 pm
by superdezign
scheinarts wrote:you mean check to see if one of the check boxes has been selected?, but i dont get how to concatenate additional string... you mean conditionals inside the sql string?
Because I was thinking doing this... if(thisCheckBox){run this sql query}
No no, like concatenating each part to the end of the query.

Code: Select all

$query = "SELECT `foo` FROM `table` WHERE";
$query .= $someCondition ? " `title` LIKE '%$title%'" : '';
$query .= ";";