<?php
require_once("classes.php");

//Start the session
session_start();
?>

<html>
<head>
<title>My Shopping Cart</title>
</head>
<body>
<h1>Cart</h1>

<?php

//If we have previously initiated a session in add.php, the shopping cart is
//defined as an associative array, $_SESSION['cart'].

//We first need to check if the $_SESSION['cart'] variable has been defined, as it
//won't be if the user has come straight to this page.

//We also need to check if there are currently any items in the shopping cart.
if (!isset($_SESSION['cart']) || (count($_SESSION['cart']) == 0)) 
{
	echo '<p>Your cart is woefully empty.</p>';
} 
else 
{
	echo '<table border="1" cellpadding="10px">
	<tr><th>Item</th><th>Unit price</th><th>No. of units</th><th>Subtotal</th></tr>';
	$total = 0;
	
	//We iterate over all the items in $_SESSION['cart'].
	//Each item represents a "row," or a single product to buy, in the shopping cart.	
	foreach($_SESSION['cart'] as $item) 
	{
		//Recall that the format of the $item associative array is the same as the one
		//defined in add.php, since the array has simply been passed to this page
		//via the $_SESSION variable.
		
		$total = $total + $item->getTotal();
?>

		<tr>
			<td><? echo $item->getName(); ?></td>
			<td>$<? echo $item->getPrice(); ?></td>
			<td><? echo $item->getQuantity(); ?></td>
			<td>$<? echo $item->getTotal(); ?></td>
			<td><a href="remove.php?item=<? echo $item->getName() ?>">remove</a></td>
		</tr>
			
<?
	}
?>
	</table>
	<p><strong>Grand total: </strong>$<? echo $total; ?></p>
<?
}
?>
<hr />
<p><a href="index.php">Go buy something</a></p>
</body>
</html>
