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

Edited by lewisia2010
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.