0

I am a little new to MySQL and PHP please help me.. I am try to do a web application where users select an ingredient (e.g..chicken) and it will show 11 recipes(I limited my recipes to 11) related to the selected ingredient.. It's working perfectly.. but I want to change it to check box so that users can select multiple ingredients (e.g.. chicken, carrot, butter).. I have no idea how to do it! Please help.. I have my database ready with all the recipes sorted out according to the Ingredient.

here is my code for the one Ingredient..

        <div class="maindiv">
        <div class="divA">
        <div class="title">

</div>
<div class="divB">
<div class="divD">
<p>Click On Menu</p>
<?php
 $connection = mysql_connect("localhost", "root", "");  
 $db = mysql_select_db("SBI", $connection); 

$query = mysql_query("select * from recipes", $connection);
while ($row = mysql_fetch_array($query)) {
echo  "<b><a href=\"CookWhatLahSBIBefore.php?id={$row['id']}\">        
{$row['name']}</a></b>";

}
?>
</div>
<?php
if  (isset($_GET['id'])) {
$id = $_GET['id'];

$query1 = mysql_query("select * from recipes where id=$id", $connection);

while (  $row1 = mysql_fetch_array($query1))  {
?>
<div class="form">
<h2>---You can cook..---</h2>

<span  style="color: #0c0f44; font-size: 15pt;text-decoration:underline;">Name:</span> <?php echo $row1['recipename']; ?><br><br>
<span style="color: #0c0f44; font-size: 15pt;text-decoration:underline;">Ingredients:</span> <?php echo  $row1['ingredients']; ?><br><br>
<span style="color: #0c0f44; font-size: 15pt;text-decoration:underline;">Recipe:</span> <?php echo $row1['recipe']; ?><br><br><br>

<span style="color: #0c0f44; font-size: 15pt;text-decoration:underline;">Name:</span> <?php echo $row1['recipename1']; ?><br><br>
<span style="color: #0c0f44; font-size: 15pt;text-decoration:underline;">Ingredients:</span> <?php echo $row1['ingredients1']; ?><br><br>
<span style="color: #0c0f44; font-size: 15pt;text-decoration:underline;">Recipe:</span> <?php echo $row1['recipe1']; ?><br><br><br>

    <span style="color: #0c0f44; font-size: 15pt;text-decoration:underline;">Name:</span> <?php echo $row1['recipename2']; ?><br><br>
<span style="color: #0c0f44; font-size: 15pt;text-decoration:underline;">Ingredients:</span> <?php echo $row1['ingredients2']; ?><br><br>
<span style="color: #0c0f44; font-size: 15pt;text-decoration:underline;">Recipe:</span> <?php echo $row1['recipe2']; ?><br><br><br>

  <span style="color: #0c0f44; font-size: 15pt;text-decoration:underline;">Name:</span> <?php echo $row1['recipename3']; ?><br><br>
  <span style="color: #0c0f44; font-size: 15pt;text-decoration:underline;">Ingredients:</span> <?php echo $row1['ingredients3']; ?><br><br>
 <span style="color: #0c0f44; font-size: 15pt;text-decoration:underline;">Recipe:</span> <?php echo $row1['recipe3']; ?><br><br>

   <span style="color: #0c0f44; font-size: 15pt;text-decoration:underline;">Name:</span> <?php echo $row1['recipename4']; ?><br><br>
<span style="color: #0c0f44; font-size: 15pt;text-decoration:underline;">Ingredients:</span> <?php echo $row1['ingredients4']; ?><br><br>
<span style="color: #0c0f44; font-size: 15pt;text-decoration:underline;">Recipe:</span> <?php echo $row1['recipe4']; ?><br><br><br>

    <span style="color: #0c0f44; font-size: 15pt;text-decoration:underline;">Name:</span> <?php echo $row1['recipename5']; ?><br><br>
<span style="color: #0c0f44; font-size: 15pt;text-decoration:underline;">Ingredients:</span> <?php echo $row1['ingredients5']; ?><br><br>
<span style="color: #0c0f44; font-size: 15pt;text-decoration:underline;">Recipe:</span> <?php echo $row1['recipe5']; ?><br><br><br>

    <span style="color: #0c0f44; font-size: 15pt;text-decoration:underline;">Name:</span> <?php echo $row1['recipename6']; ?><br><br>
<span style="color: #0c0f44; font-size: 15pt;text-decoration:underline;">Ingredients:</span> <?php echo $row1['ingredients6']; ?><br><br>
<span style="color: #0c0f44; font-size: 15pt;text-decoration:underline;">Recipe:</span> <?php echo $row1['recipe6']; ?><br><br><br>

    <span style="color: #0c0f44; font-size: 15pt;text-decoration:underline;">Name:</span> <?php echo $row1['recipename7']; ?><br><br>
<span style="color: #0c0f44; font-size: 15pt;text-decoration:underline;">Ingredients:</span> <?php echo $row1['ingredients7']; ?><br><br>
<span style="color: #0c0f44; font-size: 15pt;text-decoration:underline;">Recipe:</span> <?php echo $row1['recipe7']; ?><br><br><br>

    <span style="color: #0c0f44; font-size: 15pt;text-decoration:underline;">Name:</span> <?php echo $row1['recipename8']; ?><br><br>
<span style="color: #0c0f44; font-size: 15pt;text-decoration:underline;">Ingredients:</span> <?php echo $row1['ingredients8']; ?><br><br>
<span style="color: #0c0f44; font-size: 15pt;text-decoration:underline;">Recipe:</span> <?php echo $row1['recipe8']; ?><br><br><br>

    <span style="color: #0c0f44; font-size: 15pt;text-decoration:underline;">Name:</span> <?php echo $row1['recipename9']; ?><br><br>
<span style="color: #0c0f44; font-size: 15pt;text-decoration:underline;">Ingredients:</span> <?php echo $row1['ingredients9']; ?><br><br>
<span style="color: #0c0f44; font-size: 15pt;text-decoration:underline;">Recipe:</span> <?php echo $row1['recipe9']; ?><br><br><br>

    <span style="color: #0c0f44; font-size: 15pt;text-decoration:underline;">Name:</span> <?php echo $row1['recipename10']; ?><br><br>
<span style="color: #0c0f44; font-size: 15pt;text-decoration:underline;"> Ingredients:</span> <?php echo $row1['ingredients10']; ?><br><br>
<span style="color: #0c0f44; font-size: 15pt;text-decoration:underline;">Recipe:</span> <?php echo $row1['recipe10']; ?><br><br><br>

 </div>
<?php
}
}
?>




</div>

</div>
</div>

SQL database Do you mean like this?

srav
  • 13
  • 6
  • did you really just post an image of a white box? –  May 11 '16 at 23:44
  • @Dagon No i posted an image of how it looks.. after running the code.. – srav May 11 '16 at 23:45
  • 4
    Please dont use [the `mysql_` database extension](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), it is deprecated (gone for ever in PHP7) Specially if you are just learning PHP, spend your energies learning the `PDO` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly May 11 '16 at 23:45
  • Looks like **a white box to me** – RiggsFolly May 11 '16 at 23:45
  • ok well all i see is a white box, so something screwey is going on at my end –  May 11 '16 at 23:45
  • oh not just me, good, @RiggsFolly maybe if we zoom in real far :-) –  May 11 '16 at 23:46
  • I have to admit, its one of the nicest white boxes I have seen in a long time. – RiggsFolly May 11 '16 at 23:49
  • @RiggsFolly Thank you for the help.. :) but how do i display it in my .html page? – srav May 11 '16 at 23:49
  • Are all 11 recipes in 1 row in your database? `$row1['recipename']`/`$row1['recipename1']`/etc? And all your `$row1['ingredients']` columns are csv? You really should normalize your table, as this is going to cause you tons of issues – Sean May 11 '16 at 23:50
  • @Dagon am sorry but i don't know why the image is not been displayed.. – srav May 11 '16 at 23:50
  • @Sean Yes they are.. normalize my table as in.. ? – srav May 11 '16 at 23:51
  • 2
    You really have the knack of catching the perfect white box. The lighting, the nuiance of the post apocalyptic grace, the..... – RiggsFolly May 11 '16 at 23:52
  • But seriously Welcome to SO. Please read [What topics can I ask about](http://stackoverflow.com/help/on-topic) and [How to ask a good question](http://stackoverflow.com/help/how-to-ask) And [the perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) SO is **not a free coding or code conversion or tutorial or library finding service** You have to show that you have made some effort to solve your own problem. And I see nothing in your code that looks anything like a **checkbox**, unless you hid it behind one of your famous "White Boxes" – RiggsFolly May 11 '16 at 23:53
  • Each recipe should be its own row in the table. So you should have 11 rows, not 1 row with 33 columns (3 columns per recipe). The ingredients should be 1 to many rows in a separate table, with the recipe row id as a foreign id for the ingredients. This will make it easier to select recipes with multiple ingredients. – Sean May 11 '16 at 23:54
  • @Sean Thank you soo much.. i shall change it.. :) – srav May 11 '16 at 23:56
  • @RiggsFolly i am unable to upload.. am sorry and thanks.. – srav May 11 '16 at 23:57
  • Dont apologise. I had a great time. Sorry for the wize cracks... I just could not resists it – RiggsFolly May 11 '16 at 23:57
  • @Sean I posted a picture of my database as an answer.. can u see it.. ? – srav May 12 '16 at 00:19
  • @RiggsFolly can you see the image now..? – srav May 12 '16 at 00:22
  • You posted it as an answer. You need to edit your question an place your additional info there, not as an answer. – Sean May 12 '16 at 00:28
  • see http://stackoverflow.com/questions/13882738/designing-a-recipe-database-that-needs-to-include-ingredients-as-well-as-sub-rec to see an example of how to build your database tables. – Sean May 12 '16 at 00:30
  • yes, how disappointing. I prefer the "White Box" – RiggsFolly May 12 '16 at 00:38
  • @Sean Okay sure.. thank you for the info and the link.. :) – srav May 12 '16 at 00:38
  • @Sean I didn't understand this line.. "The ingredients should be 1 to many rows in a separate table, with the recipe row id as a foreign id for the ingredients." can u please explain.. thanks u.. – srav May 15 '16 at 00:19
  • @Sean I have tried it.. is it like that u mean..?please see the image.. – srav May 15 '16 at 00:32

0 Answers0