How to make a filtered search with mysql?

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
scheinarts
Forum Commoner
Posts: 52
Joined: Wed Jul 25, 2007 2:37 am

How to make a filtered search with mysql?

Post 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!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Check to see if each value has a value and concatenate additional conditions to the SQL string.
(#10850)
scheinarts
Forum Commoner
Posts: 52
Joined: Wed Jul 25, 2007 2:37 am

Post 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}
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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 .= ";";
Post Reply