Jump to content

Best way to write this query ?


Mesh3L
 Share

Recommended Posts

Hello,I have a form that has some options for the user (e.x, color,size and price). For every one the user can select a specific value from a menu OR select ALL (e.x: COLOR : ALL,RED,BLUE,BLACK).Now, I have one SELECT query that looks similar to (SELECT * FROM TABLE WHERE COLOR='$COLOR' AND SIZE='$SIZE' AND PRICE='$PRICE':)BUT, if for one of the options, the user selects the value "ALL". This will effect the query and return null.Is there some thing in the query that i can change to ignore that specific option if the value is ALL ?The only in hand solution i can think of now is to make an IF statements for every scenario with a separate query for each one. But this will take for ever as i have more options that mentioned.UPDATE :I found one solution by replacing ALL with '%' and than use LIKE '%' instead of the equal sign.If anyone has another suggestion please post it.Thank you

Edited by Mesh3L
Link to comment
Share on other sites

You can prepare the sql in php. You can remove the condition of color(eg COLOR='$COLOR') if user select 'all' ...else add the where clause.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...