Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Ok on my site i want to be able to have people enter 3 ingredients they have and have it search my database. Right now it brings up all recipes i have in the database, so what I am wondering is how can i get it to narrow it down to recipes. I have a search bar on my site that will let you search by category, name, ingredient and it works perfect, so I didn't think that this would be that hard to do since I just want it to be able to search an ingredient, but it is giving me all recipes here is the code I use which is similar to my search bar minus the category and name.

<<pre lang="xml">?php
echo $_POST['term'];
?>
<?php
require_once("database.php");

$term = $_POST['term'];
$sql = mysql_query("SELECT * FROM `Recipe` WHERE `ingredients` like '%$term%' or `ingredients` like '%$term%' or `ingredients` like '%$term%'") or die(mysql_error());
while ($row = mysql_fetch_array($sql))
{
    echo '<br/>';
    echo '<br/>';
    echo 'Category: '.$row['category'];
    echo '<br/>';
    echo '<br/> Recipe Name: '.$row['recipename'];
    echo '<br/>';
    echo '<br/> Ingredients: '.$row['ingredients'];
    echo '<br/>';
    echo '<br/> Preparations: '.$row['preparations'];
    echo '<br/>';
    echo '<br/> Cooking Time: '.$row['cookingtime'];
    echo '<br/>';
    echo '<br/> Servings: '.$row['servings'];
    echo '<br/>';
    echo '<br/> Optional Notes: '.$row['optionalnotes'];
    echo '<br/><br/>';
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
 <table border="0">
 <h1> Meal Finder</h1>
<h2>Enter 3 ingredients</h2>
 <tr><td>I have:</td><td>
 <input type="text" name="ingredients" maxlength="40">
 </td></tr>
 <tr><td>and:</td><td>
 <input type="text" name="ingredients" maxlength="50"></td></tr>
  <tr><td>and:</td><td>
 <input type="text" name="ingredients" maxlength="50">
 </td></tr>

 <tr> <td align="left">
 </td></tr>
 </table>
 <input type="submit" name="submit" value="Submit" />
 </form>


</body>
</html

>
Posted
Comments
Albin Abel 21-Mar-11 1:41am    
Hi Member, you haven't told anything about the combination's of the ingredients. Whether the three should be there or any two or any one?. Also you have remember that it is going to be an expensive query on the other side.

1 solution

You have one, maybe two problems that I can see immediately.

1. In the form, you have three input tags with the same name. Call them, say, ingredient1, ingredient2 and ingredient3.

2. In your query, you have WHERE `ingredients` like '%$term%' or `ingredients` like '%$term%' or `ingredients` like '%$term%'. You should replace the or with and so you select recipes that include all three ingredients specified. (If you want them to select recipes that contain ANY of the three ingredients, then leave the ors there.)

Putting these together, the query would be "SELECT * FROM `Recipe` WHERE `ingredients` like '%$ingredient1%' and `ingredients` like '%$ingredient2%' and `ingredients` like '%$ingredient3%'",
where of course you have set $ingredient1, 2, 3 from $_POST['...']

Your current query uses $_POST['term'], which is empty, so your query will select all rows in the table.

If this answers your question, vote for it, and accept the answer.

Cheers,
Peter

[edit] fixed typo in suggested SELECT [/edit]
 
Share this answer
 
v2
Comments
Member 7666290 21-Mar-11 0:44am    
ok here is what I did now and I still end up with the same thing I will put hamburger, chicken, cheese and it will pull everything from cookies on. here is my new code
[code]


<form action="
<table border="0">

Meal Finder


Enter 3 ingredients



<tr><td>I have:</td><td>

<input type="text" name="ingredients1" maxlength="40">

</td></tr>

<tr><td>and:</td><td>

<input type="text" name="ingredients2" maxlength="50"></td></tr>

<tr><td>and:</td><td>

<input type="text" name="ingredients3" maxlength="50">
</td></tr>


<tr> <td align="left">
</td></tr>

</table>
<input type="submit" name="submit" value="Submit" />
</form>




</body>
</html>[code]
Member 7666290 21-Mar-11 0:45am    
<form action="
<table border="0">

Meal Finder


Enter 3 ingredients



<tr><td>I have:</td><td>

<input type="text" name="ingredients1" maxlength="40">

</td></tr>

<tr><td>and:</td><td>

<input type="text" name="ingredients2" maxlength="50"></td></tr>

<tr><td>and:</td><td>

<input type="text" name="ingredients3" maxlength="50">
</td></tr>


<tr> <td align="left">
</td></tr>

</table>
<input type="submit" name="submit" value="Submit" />
</form>




</body>
</html>
Peter_in_2780 21-Mar-11 0:47am    
Did you also change the sql query as I suggested?
Albin Abel 21-Mar-11 1:44am    
Just to notify that like keyword is an expensive query and results in lot of records. Here is an article which shows how to rate the resulted set and find the most matched one. if you are interested. http://www.codeproject.com/KB/database/suggest_pattern_match.aspx
Peter_in_2780 21-Mar-11 1:48am    
@AlbinAbel. At the moment, I think getting it to work at all is more important than tuning for performance. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900