views:

57

answers:

3

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)?

A: 

There are several approaches to tackle this.

If you want to know if a key exists in an array you can use array_key_exists

Gunner
I don't need to know if a key exits, just if the array is there, and if the array is there modify the query string.
Ryank
A: 

You would create two arrays for possible tables and where chunks, then make the query that gathers everything:

$wheres=array();
$tables=array();
if(isset($_POST['ingredients'])) {
  $tables[]='ingredients';

  $ingredients=array_map('mysqL_real_escape_string', $_POST['ingredients']);
  $wheres[]='ingredients.list IN (\''. join(', ', $ingredients). '\')';
  //add other wheres if you want
}
if(isset($_POST['meals'])) {
  $tables[]='meal';

  $meals=array_map('mysqL_real_escape_string', $_POST['meals']);
  $wheres[]='meals.list IN (\''. join(', ', $ingredients). '\')';
  //add other wheres if you want
}

if(!$tables) {
  echo 'You have not chose anything!';
} else {
  $query = 'SELECT * FROM '. join(',', $tables). ' WHERE '. join(' AND ', $wheres);
  //do other stuff..
}
aularon
That's cool - but it doesn't scale well, let's say I could potentially return 5 arrays that would all modify the query...
Ryank
in that case make an array that keys that acceptable input $_POST variables, and have each of their values : `'meals' => array('table'=>'meal', 'fieldname'=>'meals')`, for example. and with a small simple loop you loop that array and create your query. once you wanna modify you add a new element to that array. isn't this more cool :)
aularon
A: 

I see no "all the possible combinations or possible inputs" here but just all possible inputs.
I'd check all possible inputs and add them to the query if filled.

Col. Shrapnel
I think I could use sprintf() and modify it, but I was hoping for a more scalable and elegant solution.
Ryank
@Ryank try your "5 new arrays" and you will see it's impossible to do it automated. Each parameter require it's own treatment. Not every condition can be written as simple equality check but others may use `between`, `less than`, `greater than`, `like` and such. To predefine all your conditions IS elegant. Do not forecast future usage based on such a silly example. Try to USE that more complex issue first. And only then look for solution
Col. Shrapnel