<?
	$xml = new SimpleXMLElement(file_get_contents("store.xml"));

	session_start();
	
	$cart = $_SESSION["cart"];
?>
<? require_once("begin.php"); ?>

		<table border="1" cellpadding="10">
		<? 	
			if($cart)
			{
		?>
				<th>Item</th><th>Size</th><th>Quantity</th><th>Unit Price</th><th>Item Subtotal</th>
		<?
				$keys = array_keys($cart);
				foreach($keys as $key) 
				{ 
					$array = explode(".", $key);
					$name = $array[0];
					$size = $array[1];
					$quantity = $cart["$key"];
					
					$price = $xml->xpath("//item[@name='$name']/price[@size='$size']");
					$unitPrice = (float) $price[0];
					$itemSubtotal = $unitPrice * $quantity;
					
					$total = $total + $itemSubtotal;
		?>
					<tr>
						<td><? print($name); ?></td>
						<td><? print($size); ?></td>
						<td align="right"><? print($quantity); ?></td>
						<td align="right">$<? printf("%0.2f", $unitPrice); ?></td>
						<td align="right">$<? printf("%0.2f", $itemSubtotal); ?></td>
					</tr>
		<? 		
				}
		?>
				<tr><td colspan="5" align="right"><b>Total: $<? printf("%0.2f", $total); ?></b></td></tr>
		<?
			}
			else
			{
		?>
				Your cart is empty!
		<?  }  ?>
		</table>
		
		<p><a href="checkout.php">Checkout</a></p>
		
<? require_once("end.php"); ?>
