0

I am now trying to create a page which shows restaurants in a list for people to comment and rate, the restaurant info is in the database. I've already get the data I want using while() function, but I am struggling to pick one of them and pass to another page. Below is my code. I tried to use sessions to store the data, like the "$_SESSION['rid']" for storing the restaurant ids. I have 8 rows in the restaurant table, and when I click on the first restaurant, the number shows on the other page is 8.

<?php
    $sql_restaurant = "select * from tbl_restaurants";
    $results = mysql_query($sql_restaurant);
    while($restaurant_row=mysql_fetch_assoc($results)){
      $_SESSION['rid'] = $restaurant_row['restaurant_id'];
      echo "<a><span style = 'color:black'>".$restaurant_row['restaurant_name']."</span></a>";
      echo "<dd>";
      echo "<a><span style = 'color:black'>".$restaurant_row['restaurant_address']."</span></a>";echo '</dd>';
      echo '<i class="mod_subcate_line"></i>';
      echo '<a href = "rating.php">Rate it!</a>';
      echo '<a href = "comment.php">Comment!</a>';
      echo '<br/>';
      echo '<br/>';
    }
?>

I want it to show the right restaurant id when I click on different restaurants. How can I solve this?

2
  • 2
    you will want to use a $_GET variable ie. href="resturant.php?id=1" where id=1 is your restaurant_id Commented Nov 26, 2014 at 20:48
  • 1
    As you fetch the restaurant data you're overwriting the $_SESSION variable, so the value in there will always be the last one. You need to pass the value as a querystring value or possibly as a POST value. Commented Nov 26, 2014 at 20:50

2 Answers 2

1

Right now your code is changing $_SESSION['rid'] every time you cycle through the loop. So it will have the last cycle of the loop.

You shouldn't do this with sessions, always keep in mind that HTTP is a stateless system and sessions are stateful. It's always better to not rely on state.

As mentioned in the comments, you should write the restaurant ID as part of the URL of the link in a query parameter that you can then access through the $_GET array.

Sign up to request clarification or add additional context in comments.

Comments

0

If you want to use $_SESSION, choose a different name for every loop. Here, $_SESSION['rid'] get the value of the last loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.