Hypothetically I three tables: * recipes - contains recipes * ingredients - list of items * meals - list of meals
I have a form that generates a selection like:
Choose what ingredients you have:
- apples
- bananas
- cherries
Choose the meal this is for:
- breakfast
- lunch
- dinner
I want the user to be able to choose from either or none of the above, i.e they may choose apples OR cherries OR (bananas && lunch)
When I query MySQL my query is roughly
select recipes.* from recipes
or
select recipes.* from recipes, ingredients
where recipes.id= ingredients.id and ingredients.list in ('apple');
or
select recipes.*
from recipes, ingredients, meal
where recipes.id= ingredients.id
and ingredients.list
and ingredients.id = meals.id
and ingredients.list ('apple')
and meals.list in ('lunch');
Is there a nice way of saying (in PHP) if this array exists (i.e. is_array(ingredients) add to the query the table (ingredients) and at the end tack on (".ingredients.list in ('apple'))...
without having to write all the possible combinations or possible inputs (i.e. the user's selected from the ingredients list, or from the ingredients and meals list, or from no lists)?