removeAll(); $_SESSION['cart'] = serialize($cart); header('Location: shop.php'); end(); } // item parameter indicates an attempt to add or remove items if (isset($_GET['item'])) { // verify item is valid $query = sprintf('SELECT ITEM_ID FROM %sSHOP_INVENTORY WHERE ' . 'ITEM_ID = %d', DB_TBL_PREFIX, $_GET['item']); $result = mysql_query($query, $GLOBALS['DB']); if (mysql_num_rows($result)) { $row = mysql_fetch_assoc($result); $item = $row['ITEM_ID']; // add item to cart if (isset($_GET['add'])) { $cart->addItem($item); } // remove item from cart else if (isset($_GET['remove'])) { $cart->removeItem($item); } } mysql_free_result($result); // save cart to session and redirect to the previously viewed page $_SESSION['cart'] = serialize($cart); header('Location: ' . htmlspecialchars($_SERVER['HTTP_REFERER'])); exit(); } // view shopping cart's contents else { // update item quantities in shopping cart if (isset($_GET['update'])) { foreach ($_POST['qty'] as $item => $qty) { $cart->addItem($item, $qty); } } ob_start(); echo '

Your Cart

'; echo '

Back to all categories'; // verify category parameter and construct suitable back link if passed if (isset($_GET['category'])) { $query = sprintf('SELECT CATEGORY_ID, CATEGORY_NAME FROM ' . '%sSHOP_CATEGORY WHERE CATEGORY_ID = %d', DB_TBL_PREFIX, $_GET['category']); $result = mysql_query($query, $GLOBALS['DB']); if (mysql_num_rows($result)) { $row = mysql_fetch_assoc($result); echo ' / Back to ' . $row['CATEGORY_NAME'] . ''; } mysql_free_result($result); } echo '

'; if ($cart->isEmpty) { echo '

Your cart is empty.

'; } else { // display empty cart link echo '

'; echo 'Empty Cart

'; // encapsulate list in form so quantities may be changed echo '
'; // list each item in the cart, keeping track of total price $total = 0; echo ''; echo ''; foreach ($cart->contents as $id => $qty) { $query = sprintf('SELECT ITEM_NAME, PRICE FROM %sSHOP_INVENTORY ' . 'WHERE ITEM_ID = %d', DB_TBL_PREFIX, $id); $result = mysql_query($query, $GLOBALS['DB']); $row = mysql_fetch_assoc($result); echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; $total += $row['PRICE'] * $qty; mysql_free_result($result); } echo '
ItemQtyPriceTotal
' . $row['ITEM_NAME'] . '$' . number_format($row['PRICE'], 2) . '$' . number_format($row['PRICE'] * $qty, 2) . '
'; echo ''; echo '

Total Items: ' . $cart->totalItems . '
'; echo 'Total Quantity: ' . $cart->totalQty . '

'; echo '

Total Price: $' . number_format($total, 2) . '

'; // display link to checkout echo '

'; echo 'Proceed to Checkout

'; } // save cart to session and display the page $_SESSION['cart'] = serialize($cart); $GLOBALS['TEMPLATE']['content'] = ob_get_clean(); include '../templates/template-page.php'; } ?>