创建一个循环来显示具有相同成分的食谱

时间:2017-04-03 19:21:43

标签: php html sql database loops

所以我创建了一个网站,允许用户查看从3个下拉菜单中选择的食材的配方:肉类,水果和蔬菜和碳水化合物。一旦用户从上面的下拉菜单中选择了3种成分,就应该显示一个配方页面,其中包含适合这些选择的配方。

我到目前为止所有工作:数据库,用户选择和显示配方。虽然我想为某些食材添加多种配方,例如如果用户选择了碎肉,西红柿和意大利面,我怎样才能展示意大利肉酱和烤宽面条? - 我需要一个循环来创建这个但不确定如何。非常感谢任何帮助。

到目前为止

代码:

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">

<title>Economic Eating</title>

<!-- Bootstrap Core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">

<!-- Custom CSS -->
<link href="css/modern-business.css" rel="stylesheet">

<!-- Custom Fonts -->
<link href="font-awesome/css/font-awesome.min.css" rel="stylesheet"     type="text/css">

<body>
 <!-- Navigation -->
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
    <div class="container">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a href="index.php" class="pull-left"> <img src="images/logo1.jpg"></a> 
        </div>
        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav navbar-right">
                <li><a href="favouriteRecipes.html">Favourite Recipes</a>
                </li>
                <li>
                    <a href="addRecipe.php">Add Recipe</a>
                </li>
            </ul> 
        </div>
        <!-- /.navbar-collapse -->
    </div>
    <!-- /.container -->
</nav>

<!-- Page Content -->
<div class="container">



    <!-- Page Heading/Breadcrumbs -->
    <div class="row">
        <div class="col-lg-12">
            <br><br>

require 'dbConnect.php';

$meat = $_POST['ingred1'];
$fruitveg = $_POST['ingred2'];
$carbs = $_POST['ingred3'];

$ingredientsQuery = $db->prepare("SELECT recipe.id AS temp, ingredients, meat_qty, fruitveg_qty, carbs_qty, FROM meat, fruitveg, carbs, ingredients, recipe WHERE recipe.id = ingredients.recipe_id and meat.ingredName = '$meat' and meat.meat_id = recipe.meat_id and fruitveg.fruitvegName = '$fruitveg' and fruitveg.fruitveg_id = recipe.fruitveg_id and carbs.carbName = '$carbs' and carbs.carbs_id = recipe.carbs_id");
$ingredientsQuery->execute();



$recipeNameQuery = $db->prepare("SELECT recipeName 
                                 FROM meat, fruitveg, carbs, recipe 
                                 WHERE meat.ingredName = '$meat' and meat.meat_id = recipe.meat_id and
                                       fruitveg.fruitvegName = '$fruitveg' and fruitveg.fruitveg_id = recipe.fruitveg_id and
                                       carbs.carbName = '$carbs' and carbs.carbs_id = recipe.carbs_id");
$recipeNameQuery->execute();

$recipeQuery = $db->prepare("SELECT ingredName, fruitvegName, carbName, recipeName 
                             FROM meat, fruitveg, carbs, recipe
                             WHERE meat.ingredName = '$meat' and meat.meat_id = recipe.meat_id and
                                   fruitveg.fruitvegName = '$fruitveg' and fruitveg.fruitveg_id = recipe.fruitveg_id and
                                   carbs.carbName = '$carbs' and carbs.carbs_id = recipe.carbs_id");
$recipeQuery->execute();

$instructionQuery = $db->prepare("SELECT instruction
                                 FROM meat, fruitveg, carbs, instruction, recipe
                                 WHERE recipe.id = instruction.recipe_id and
                                 meat.ingredName = '$meat' and meat.meat_id = recipe.meat_id and
                                 fruitveg.fruitvegName = '$fruitveg' and fruitveg.fruitveg_id = recipe.fruitveg_id and
                                 carbs.carbName = '$carbs' and carbs.carbs_id = recipe.carbs_id");
$instructionQuery->execute();


$photoQuery = $db->prepare("SELECT photo, ingredName, fruitvegName, carbName
                            FROM meat, fruitveg, carbs, recipe
                            WHERE meat.ingredName = '$meat' and meat.meat_id = recipe.meat_id and
                                   fruitveg.fruitvegName = '$fruitveg' and fruitveg.fruitveg_id = recipe.fruitveg_id and
                                   carbs.carbName = '$carbs' and carbs.carbs_id = recipe.carbs_id");
$photoQuery->execute();



while  ($meat = $_POST['ingred1'] && $fruitveg = $_POST['ingred2'] && $carbs = $_POST['ingred3']) {
  if ($recipeName = $recipeNameQuery->fetch(PDO::FETCH_ASSOC)) 
                        {?>
    <h3><b><?php echo $recipeName['recipeName'];} ?></b></h3><br>
        <ol class="breadcrumb">
            <li><a href="index.php">Home</a></li>
            <li><a href="favouriteRecipes.html">Back to Favourite Recipes</a></li>
        </ol>
        </div>
    </div>
<!-- /.row -->

<!-- Portfolio Item Row -->
<div class="row">

    <div class="col-md-8">
        <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<!-- Wrapper for slides -->
            <div class="carousel-inner">
                <div class="item active"><br>
                     <img class="img-responsive" src="<?php
                        if ($photo = $photoQuery->fetch(PDO::FETCH_ASSOC)){
                            echo $photo['photo'];}?>" alt="">
                </div>
            </div>
        </div>
    </div>

<div class="col-md-4">
    <h3><u>Recipe Ingredients</u></h3>      
        <ul>
        <option value="<?php echo $ingredients['id'];?>"></option>
                <li><b><?php echo $_POST["ingred1"]; ?></li>
                <li><?php echo $_POST["ingred2"]; ?></li>
                <li><?php echo $_POST["ingred3"]; ?></li></b>
                <?php
                while ($ingredients = $ingredientsQuery->fetch(PDO::FETCH_ASSOC)) {?>
                    <?php if ($ingredients['temp'] == ['ingredients.recipe_id']){?>
                <li><?php echo $ingredients['temp'];
                echo $ingredients['ingredients']; ?></li><?php } }?>
        </ul>
</div>
</div>
<!-- /.row -->

<!-- Related Projects Row -->
<div class="row">
    <div class="col-lg-12">
        <h3 class="page-header">Instructions</h3>
            <?php
            while ($instruction = $instructionQuery->fetch(PDO::FETCH_ASSOC)) {
            ?>
                <ul style="list-style-type:square"><li> <?php echo $instruction['instruction']; ?><br><br></li></ul>
            <?php }
}               ?>
    </div>  
</div>

0 个答案:

没有答案