<?php
//Start the session
session_start();
?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<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">
    <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.
    
        
echo "<tr><td>{$item['item']}
        </td><td>\$
{$item['unitprice']}</td><td>{$item['quantity']}</td>
        <td>$"
.($item['unitprice'] * $item['quantity'])."</td></tr>";
        
$total += ($item['unitprice'] * $item['quantity']);
    }
    echo 
'</table>';
    echo 
"<p><strong>Grand total: </strong>\$$total</p>";
}
?>
<hr />
<p><a href="buy.php">Go buy something</a></p>
</body>
</html>