I have a code like this and variables which are selected by user in check-able form
SELECT
FROM
WHERE
product = 'Super Model'
AND Model = 'Model1'
OR Model = 'Model2'
OR Model = 'Model3'
OR Model = 'Model4'
GROUP BY
And check list look like:
Choose Models:
- Model1
- Model2
- Model3
- Model4
I want to make this query dynamic. I would make a switch case statement for this.
switch ($model) {
case "Model1":
$models = " AND Model = 'Model1' ";
break;
case "Model2":
$models = " OR Model = 'Model2' ";
break;
case "Model3":
$models = " OR Model = 'Model3' ";
break;
case "Model4":
$models = " OR Model = 'Model4' ";
break;
}
But there are problems: 1. It works only for one variable 2. If a user will choose i.e. model2 and model3 string will begin with OR statement, which is not logical as it suppose to start with AND.
I tried with something like this:
$models = " AND '.$model[0].' OR '.$model[1].' OR '.$model[2].' OR '.$model[3].' OR '.$model[4].' ";
then I put $models in WHERE statement but it doesn't work. And I have noticed problems: 1. If user will select only 2 models, probably statement will look like: " AND Model2 OR Model3 OR OR OR " 2. It is not dynamic, I would like to have something like: "for every item in the array do sth".
So maybe foreach? But how to deal with this AND at the beginning and OR between other models?
Any help appreciated.
INstatement in your query;. It would be something likeAND Model in ('model1', 'model2', 'model3')and so on.$modelan array ?