Lawrence Smith
Lawrence Smith

Reputation: 337

Using session id's to grab data

I've been working on a project for a few weeks where a user chooses a set of ingredients and it pulls out the recipes that have them ingredients in, but now I have a page that displays the recipes and I want the user to be able to click on a specific recipe and be directed to a page that displays that recipe and it's instructions.

Would I need to use a session id?

This displays the results and the buttons that must be pressed to get to the recipes instructions page. enter image description here

This is the page that you are directed to and it will display the recipe image, name, ingredients and instructions.

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>';


    }
}


?>

This is the php for grabbing the information from the database.

Upvotes: 0

Views: 168

Answers (2)

Sean
Sean

Reputation: 12433

Use the recipe id, instead of a session id -

first add id to your query

// 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.")

then put that id in your form action link -

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

and then on recipe.php you can get that id to use in your query

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

Upvotes: 0

CoderOfHonor
CoderOfHonor

Reputation: 741

I am assuming that all of your recipes have id numbers in your database. You could just use $_GET variables so that each 'look' button would be a link containing something like:

<?php

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

?>

That way, the user could bookmark a recipe page or share the page with others easily. Then on the details.php page you just need to look in the database for a recipe with the specified id.

Upvotes: 2

Related Questions