Jump to content

Specific search query using php and database array


lewisia2010

Recommended Posts

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.

<?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 '<br />You did not select any ingredients. Please try again<br /><br />
	      <button type="button" onclick="history.back();">Back</button>';
		  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
"<div class='recipeResults'><a href='recipe.php?recipe=".$row['recipe_name']."'>". $row['recipe_name']. "<br />
<img src='<!--MY URL-->
".$row['recipe_image']."' 
alt='Picture of ".$row['recipe_name']."' title='Picture of ".$row['recipe_name']."' /></a></div>";
		}


}}}

if(!$recipes) {
	echo '<br />No recipes found using the ingredients selected. Please try again<br /><br />
	      <button type="button" onclick="history.back();">Back</button>';
}
		
?>

Thanks for your time :)

For once the forum software was useful :-P

 

This isn't exactly your question, but please read it and see if you can figure out how to change your approach. http://forums.phpfreaks.com/topic/274119-matching-a-one-to-many-relationship-exactly/

Thanks for your reply. I am extremely new to PHP so a lot of the concepts are confusing to me. So far I have basically stumbled upon a method which works to a degree for me. Using new coding methods will be extremely difficult for meat this stage. But Ill have a look at the link you posted and if you think of anything in the mean time I will appreciate it.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.