2013-05-07 90 views
0

我一直在一个项目上工作了几个星期,用户选择一组成分,它提取出有成分的食谱,但现在我有一个页面显示食谱,我希望用户能够点击一个特定的食谱,并被引导到一个页面,显示该食谱和它的说明。使用会话ID来抓取数据

我需要使用会话ID吗?

这会显示结果和必须按下的按钮才能进入配方说明页面。 enter image description here

这是您定向到的页面,它将显示食谱图像,名称,配料和说明。

enter image description here

<?php 
if (isset($_POST['search'])) { 
    $ingredient1 = mysql_real_escape_String ($_POST['dropdown1']); 
    $ingredient2 = mysql_real_escape_String ($_POST['dropdown2']); 
    $ingredient3 = mysql_real_escape_String ($_POST['dropdown3']); 


    $recipes = mysql_query(" 
     SELECT DISTINCT `name`, `step1`, `step2`, `step3`, `image`, `reference` 
     FROM `recipe` r 
     INNER JOIN `recipe_ingredients` ri 
     ON r.id = ri.recipe_id 
     WHERE ri.ingredient_id IN (".$ingredient1.",".$ingredient2.",".$ingredient3.") 
    "); 

    while ($recipe = mysql_fetch_assoc($recipes)) { 

     echo '<section id="row">'; 
     echo '<br />'; 
     echo $recipe['name'].'<br />'; 
     echo '<br />'; 
     echo "<img src=\"{$recipe['image']}\" height=100 width=100 /><br />"; 
     echo '<form action="recipe.php">'; 
     echo '<input id="search" name="Look" type="Submit" value="Look" >'; 
     echo '</form>'; 
     echo '<br />'; 
     echo 'You will Need: '; 
     echo '<br />'; 
     echo '</section>'; 

     //echo 'Step 1: '; 
     //echo $recipe['step1'].'<br />'; 
     //echo '<br />'; 
     //echo 'Step 2: '; 
     //echo $recipe['step2'].'<br />'; 
     //echo '<br />'; 
     //echo 'Step 3: '; 
     //echo $recipe['step3'].'<br />'; 
     //echo '<br />'; 
     //echo '<br />'; 
     //echo '<a href="http://'.$recipe['reference'].'">'.$recipe['reference'].'</a>'; 
     //echo '</li>'; 
     //echo '</ul>'; 


    } 
} 


?> 

这是抓住从数据库中信息的PHP。

+0

是您的实际问题“我需要使用会话ID吗?” – sircapsalot 2013-05-07 19:13:59

+0

@sircapsalot我想知道这是否是完成此操作的最佳方法。 – 2013-05-07 19:16:30

+1

从表格中提取配方'id'以及步骤,或者使'Look'按钮成为一个链接(例如到'recipe.php?id = <?php echo $ recipe ['id'];?>')或者保留一个表单并添加一个隐藏字段:' />”'。根据您的表单方法('get'或'post'),您可以在_recipe.php_中以'$ _GET ['id']'或'$ _POST ['id']'的身份访问id。 – Lukas 2013-05-07 19:22:05

回答

2

我假设你所有的食谱在你的数据库中都有ID号。你可以只使用$_GET变量,使每个“看”按钮,将包含类似的链接:

<?php 

//Link part of message 
echo "<a href='/recipies/detail.php?id=" . $recipe['id'] . "'>" . $recipe['name'] . "</a>"; 

?> 

通过这种方式,用户可以收藏这个配方页面或与他人轻松共享页面。然后在details.php页面上,您只需在数据库中查找具有指定标识的配方。

+0

谢谢,我的食谱确实有ID,我会试试这个。我还是新手,所以我会更多地考虑$ _GET变量。 – 2013-05-07 19:45:42

+0

我很高兴这个解决方案适合你。确保选择最能帮助您的答案(通过点击答案等级号码下方的检查)。 – CoderOfHonor 2013-05-07 19:50:07

0

使用配方ID,而不是会话ID -

首先添加id到查询

// add your `recipe`.`id` to your search query 
SELECT DISTINCT `id`, `name`, `step1`, `step2`, `step3`, `image`, `reference` 
    FROM `recipe` r 
    INNER JOIN `recipe_ingredients` ri 
    ON r.id = ri.recipe_id 
    WHERE ri.ingredient_id IN (".$ingredient1.",".$ingredient2.",".$ingredient3.") 

然后把该ID在你的表单操作链接 -

echo "<form action=\"recipe.php?id={$recipe['id']}\">"; 

和然后在recipe.php你可以得到id用于你的查询

$recipe_id = mysql_real_escape_string ($_GET['id']);