Ok, I have this dilemma reguarding the building of querries.
I have a website that browses hotels. The user can select the region and the quality of the hotel via some dropdown boxes and then he is offered weather the search results should include certain options that the hotels should have... like "parking space" or "swimming pool".
I use checkboxes for each of these options. Now my problem is that I don't have the slightest idea how to handle the building of the querries in order to include these checkboxe options.
Each checkbox, when checked, is supposed to add an AND () statement to the querry's condition parameters. But if the checkboxes aren't selected that condition should not exist.
the inefficient way would be to build an extensive IF structure that would encompas all possible combinations of the checkboxes. but if you have, for example, 9 checkboxes that would result in 81 posible combinations of the querry. if you also combine that with the extra combinations added from the dropdown boxes you get madness...
There has to be an easyer way!
The querry structure when ALL the checkboxes are unselected looks something like this:
$sql_querry="SELECT DISTINCT tables.fields
FROM tables
WHERE ((condition )
AND (condition))
ORDER BY something ASC";
now I would like to do something like this:
$sql_querry="SELECT DISTINCT tables.fields
FROM tables
WHERE ((condition )
IF ( checkbox1 is set )
{
AND ( condition )
}
IF ( checkbox2 is set )
{
AND ( condition )
}
etc.
AND (condition))
ORDER BY something ASC";
Now obviously just sticking the IF statements in the querry's parameters won't work.
I also thought of adding the IF statement inside the AND structure like this:
$sql_querry="SELECT DISTINCT tables.fields
FROM tables
WHERE ((condition )
AND (
IF ( checkbox1 is set )
{
condition
}
)
AND (condition))
ORDER BY something ASC";
Obviously that didn't work either.
Can anybody give me some ideas reguarding what I could do about this? there has to be a way easyer then just doing 100 combinations for each combination of the checkboxes.