Specific search query using php and database array

Hi. PHP n00b here.

Basically I have been building a recipe application over the past few months and have come unstuck.

Client side, users select some ingredients using checkboxes. When they submit the form, these ingredients are sent as an array for processing.

A results page loads with the following happening behind the scenes:

A query compares the posted ingredient array against the list of ingredients in the database. These ingredients are then echoed to the results page.

Maybe you wont need this information, just thought I would bring viewers up to speed.

Now…

At the moment if 20 ingredients are selected by the user, recipes will appear in the search even if they only contain one of those ingredients. I want this php to make the search specific in that only recipes with all of the ingredients featured will be returned.

How do I make this possible?

Here’s my source code for the PHP page.

[code]<?php

include ‘connect.php’;

//loops through the ingredients in the ingredients array from the checkboxes.
//uses a variable as a reference

$recipeImage = array();
$recipes = array();
$ingredientArray = ($_GET[‘ingredients’]);

if(!$ingredientArray) {
echo ‘
You did not select any ingredients. Please try again


Back’;
exit();
}
else {

//for each checkbox, post as $ingr
foreach($ingredientArray as $ingr){

//select from database where $ingr = ingredient name
$result = mysql_query(“SELECT * FROM recipe, ingredient, recipe_association WHERE ingredient.ingredient_name = ‘$ingr’ AND ingredient.ingredient_id=recipe_association.ingredient_id AND recipe_association.recipe_id=recipe.recipe_id”) or die(mysql_error());

//while $row = query result (selected ingredients)
while($row = mysql_fetch_array($result)){
$check = false;
//check for duplicates in the array
foreach($recipes as $recipe){
if($row[‘recipe_name’]==$recipe){
$check = true;
}
}

//if they haven't been added yet, add them
	if(!$check){	

		array_push($recipes, $row['recipe_name']);

echo

<a href='recipe.php?recipe=”.$row[‘recipe_name’]."’>". $row[‘recipe_name’]. "

<img src=’
“.$row[‘recipe_image’].”’
alt=‘Picture of “.$row[‘recipe_name’].”’ title=‘Picture of “.$row[‘recipe_name’].”’ />
";
}

}}}

if(!$recipes) {
echo ‘
No recipes found using the ingredients selected. Please try again


Back’;
}

?>[/code]

Sponsor our Newsletter | Privacy Policy | Terms of Service