duanpan3166 2017-04-03 19:21
浏览 37

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

So I'm creating a website that allows users to view recipes for the ingredients chosen from 3 drop down menus: meat, fruit&veg and carbs. Once the user has chosen 3 ingredients from the above drop downs they should be shown a recipe page with recipes suited to these chosen.

I have all working so far: database, user selection and displaying recipe. Although I would like to add multiple recipes for certain ingredients, for example if mince, tomatoes and pasta have been selected by the user, how can i display both spaghetti bolognese and lasagne? - I need a loop to create this but unsure how. any help is greatly appreciated.

Code so far:

<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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了
    • ¥50 切换TabTip键盘的输入法
    • ¥15 可否在不同线程中调用封装数据库操作的类