Im having some problems looping over a bunch of objects ive store in my session to retrieve info on items in a shopping cart.
However, when i loop and display the code only displays variables for the most recent item added to the session.
The correct number of rows in the table appear though suggesting the items are there but that their values or blank or not being accessed.
Please see code below, any ideas welcome
cart-code.php
[php]
[/php]
cart.php
[php]
<?php
// Include code for shopping cart
include ("cart-code.php");
?>
<?php
include ("layout.html");
?>
<h1>Your Basket</h1>
<div id="main-body">
<?php
if($_SESSION['cart']) { //if the cart isn't empty
//show the cart
echo "<table id="cart">";
// Iterate through items in the cart
foreach($_SESSION['cart'] as $item_id => $this_item) {
$line_cost = $this_item->price * $this_item->quantity;
$total = $total + $line_cost; //add to the total cost
echo "<tr>";
echo "<td align="center">$this_item->name</td>n";
echo "<td><div style="padding-left: 4px;">".$this_item->details."</div></td>";
echo "<td align="center">x ".$this_item->quantity."</td>";
echo "<td align="center"><a href="".$_SERVER[PHP_SELF]."?action=remove&item_id=".$item_id."">remove</a></td>";
echo "<td align="center">£";
printf ("%01.2f", $line_cost);
echo "</td>";
echo "</tr>";
}
//show the total
echo "<tr>";
echo "<td colspan="4" align="right"><strong>Total </strong></td>";
echo "<td align="center"><strong>£";
printf ("%01.2f", $total);
echo "</strong></td>";
echo "</tr>";
//show the empty cart link - which links to this page, but with an action of empty.
echo "<tr>";
echo "<td colspan="5"><div style="float: left;"><a href="$_SERVER[PHP_SELF]?action=empty" onclick="return confirm('Empty all items from cart?');">Empty Basket</a></div>
<div style="float: right;">
<form style="margin-top: 0; margin-bottom: 0;" action="checkout.php" method="post">
<input type="submit" name="submit" value="Checkout" />
</form>
</div>
</td>";
echo "</tr>";
echo "</table>";
} else{
//otherwise tell the user they have no items in their cart
echo "<p>You have no items in your basket.</p>";
}
?>
</div>
[/php]
Thanks in advance!