Jump to content

Best way to write this query ?

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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...