Problem looping over session items

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 class item { var $name; var $price; var $quantity; var $details; function Item($n, $q, $p, $d) { $this->name = $n; $this->quantity = $q; $this->price = $p; $this->details = $d; } function addDetails($add) { $this->details = $this->details." - ".$add; } function incQuantity() { $this->quantity = $this->quantity + 1; } function decQuantity() { $this->quantity = $this->quantity - 1; } } ?> <?php $action = $_REQUEST['do']; switch($action) { //decide what to do case "add": $item_id = strtotime("now"); $item = new Item($_REQUEST['name'], $_REQUEST['quantity'], $_REQUEST['price'], $_REQUEST['details']); // Create and store item in session $_SESSION['cart'][$item_id] = $item; break; case "remove": $item_id = $_REQUEST['item_id']; unset($_SESSION['cart'][$item_id]); break; case "empty": unset($_SESSION['cart']); break; } ?>

[/php]

cart.php
[php]

<?php session_start(); ?> Dpi Solutions - Basket
<?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 &nbsp;</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!

Not exactly a solution, but wouldn’t it be easier to make a cart class, store each item in an array in a cart object and then use the cart object in your session?

Sponsor our Newsletter | Privacy Policy | Terms of Service