kjm7267 — 2013-09-12T03:55:48-04:00 — #1
Greetings from Sweden, where I'm stuck on something that the sharper among you will correctly recognize as pretty basic! I appreciate very much your help.
Here's a simplified version of the problem. On one page, there's a simple advanced search function where users can specify one, both or neither of two fields, each with two drop-down menu options:
<form action="results.php" method="POST">
Some records in the database have only a size specified; some have only a color specified; some have both; and importantly, some have neither.
Where I'm stuck is in writing code that returns results as follows:
If the user selects a size, all returned results should have that size.
If the user selects a color, all returned results should have that color.
If the user selects both size and color, all returned results should have both that size and that color.
If the user selects neither size nor color, then there should be no results, even those records that themselves have neither size nor color specified.
I suspect that success will come from a well written query line, which the following disaster of mine clearly is not:
$query="SELECT * FROM table WHERE size='$size' OR color='$color'";
I have read through these forum posts and have tried to better understand PHP operators, but truly am stuck. Thank you for reading!:)
pbsonawane — 2013-09-12T05:01:16-04:00 — #2
it will be better if you write two different queries as
if ($size != '')
$query="SELECT * FROM table WHERE size='$size'";
if ($color!= '')
$query="SELECT * FROM table WHERE color='$color'";
then u can merge or return the two results
kjm7267 — 2013-09-12T05:48:04-04:00 — #3
Thank you, pbsonawane, so very much for your suggestion! I see your point. But please, I think there's a problem with that. Consider a case where the user selects "small" for size and "green" for color. The returned results from the code you suggest would include all records where the size is "small," including those where the color is "blue." Please, isn't that right?
solidcodes — 2013-09-12T05:59:05-04:00 — #4
Why not post your codes here.
So everyone can see.
By the way your problem is just all about using conditions if or else or where etc...
Are you using a framework or just pure PHP?
pbsonawane — 2013-09-12T06:07:26-04:00 — #5
$query="SELECT * FROM table WHERE 1=1";
if ($size != '')
$query .= " AND size='$size'";
if ($color!= '')
$query .= " AND color='$color'";
kjm7267 — 2013-09-12T06:12:46-04:00 — #6
Thank you, solidcodes, for your comment. I am using pure PHP. And you're right, my issue does seem to be just getting the conditional statements right. In English, what I want the code to say is:
If only size is selected, return records with that size exactly;
If only color is selected, return records with that color exactly;
If both a size and a color are selected, return only records with both that size and that color exactly; and
If neither a size nor a color are selected, return no records.
The trick, I think, is not returning records that don't have any size defined when the user doesn't select a size, but selects instead only color.
Thank you solidcodes!
kjm7267 — 2013-09-13T05:06:59-04:00 — #7
Thank you so much, pbsonawane ! Your suggestion worked perfectly, and the problem is solved. I'm grateful for your help. Thanks again pbsonawane ! :tup: