lewisia2010 Posted April 16, 2013 Share Posted April 16, 2013 (edited) 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 April 16, 2013 by lewisia2010 Quote Link to comment https://forums.phpfreaks.com/topic/277027-specific-search-query-using-php-and-database-array/ Share on other sites More sharing options...
Jessica Posted April 16, 2013 Share Posted April 16, 2013 Dear lord. You are going about that in the wrong way. I'm going to have to look back a ways but I posted a question a few months ago and Barand helped me out, let me find it. It will help you. Quote Link to comment https://forums.phpfreaks.com/topic/277027-specific-search-query-using-php-and-database-array/#findComment-1425219 Share on other sites More sharing options...
Jessica Posted April 16, 2013 Share Posted April 16, 2013 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/ Quote Link to comment https://forums.phpfreaks.com/topic/277027-specific-search-query-using-php-and-database-array/#findComment-1425220 Share on other sites More sharing options...
lewisia2010 Posted April 17, 2013 Author Share Posted April 17, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/277027-specific-search-query-using-php-and-database-array/#findComment-1425229 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.