Unanswered: Shopping cart output repeating first entry
I have set up a shopping cart on a website that I'm building. I can easily add items, remove items and clear the cart. What I'm having a problem with is actually displaying the items in the cart, or more to the point the relevant database entries.
The PHP session variable holds numbers which relate to ID numbers used to identify items in a database. I need the cart to then show an image, a name, a delete option and the id number. When displaying the cart if there is more than one item the image and name are always the first item in the cart repeated for each entry, while the id number does change.
This makes me think that I am only performing one query when in fact I need to run the query inside the foreach loop of my first code segment below. As a bit of a newby to PHP, I'm not too sure the best way to go about this. I've just added a recordset via Dreamweaver (second code).
Could someone point in the best direction for guiding me to solve my little problem.
er..... I assumed from the query I run at the top of the page. It's always the first thing I add to the cart that is repeated. And here was me being pleased that I done all this bit without your help :-(
Which is assigning the value of $row_ships to variables on each pass.
However on each pass the value of $row_ships is not being changed!
Have a play with this [untested] code:
// Assigning a value to your session variable
$_SESSION['cart'] = "1,2,3,4,5";
// Implementing this in your SQL statement (filter to only return those items in the cart)
// !!! IMPORTANT !!! Remember to check against SQL injection!!
$sql = "SELECT ship_id, ship_name, image FROM ship_infomation WHERE ship_id IN (" . $_SESSION['cart'] . ") ORDER BY ship_name;";
// Show the SQL statement
echo $sql . "<br /><br /><br />";
// Connect to database
// Perform query and assign results to $ships variable
$ships = mysql_query($sql, $ships) or die(mysql_error());
// For each row in the recordset
while($row = mysqli_fetch_array($ships))
// Output the values to screen
echo $row['ship_id'] . " " . $row['ship_name'] . " " . $row['image'] . "<br />";