professional-sql-08-public-files-cart.php / php
<?php include '../lib/common.php'; include '../lib/db.php'; include '../lib/ShoppingCart.php'; // create or resume session and retrieve shopping cart session_start(); if (isset(cart = unserialize(cart = new ShoppingCart(); } // empty the shopping cart and redirect user to list of categories if (isset(cart->removeAll(); cart); header('Location: shop.php'); end(); } // item parameter indicates an attempt to add or remove items if (isset(// verify item is valid query = sprintf('SELECT ITEM_ID FROM\ %sSHOP_INVENTORY WHERE ' . 'ITEM_ID =\ %d', DB_TBL_PREFIX, result = mysql_query(GLOBALS['DB']); if (mysql_num_rows(row = mysql_fetch_assoc(item = // add item to cart if (isset(_GET['add'])) { item); } // remove item from cart else if (isset(cart->removeItem(result); // save cart to session and redirect to the previously viewed page cart); header('Location: ' . htmlspecialchars(// view shopping cart's contents else { // update item quantities in shopping cart if (isset(_GET['update'])) { foreach (item => cart->addItem(qty); } } ob_start(); echo '<h1>Your Cart</h1>'; echo '<p><a href=<shop.php>>Back to all categories</a>'; // verify category parameter and construct suitable back link if passed if (isset(query = sprintf('SELECT CATEGORY_ID, CATEGORY_NAME FROM ' . '\ %sSHOP_CATEGORY WHERE CATEGORY_ID =\ %d', DB_TBL_PREFIX, result = mysql_query(GLOBALS['DB']); if (mysql_num_rows(row = mysql_fetch_assoc(row['CATEGORY_ID'] . '">Back to ' . </a>'; } mysql_free_result(result); } echo '</p>'; if (<p><b>Your cart is empty.</b></p>'; } else { // display empty cart link echo '<p><a href="cart.php?empty">'; echo '<img src="img/cartempty.gif" alt="Empty Cart"/></a></p>'; // encapsulate list in form so quantities may be changed echo '<form method="post" action="cart.php?update'; // if a category was passed and was validated successfully earlier // then append it to the action url so the back link remains available if (isset(row['CATEGORY_ID'])) { echo '&category=' . // list each item in the cart, keeping track of total price total = 0; echo '<table>'; echo '<tr><th>Item</th><th>Qty</th><th>Price</th><th>Total</th></tr>'; foreach (id => query = sprintf('SELECT ITEM_NAME, PRICE FROM\ %sSHOP_INVENTORY ' . 'WHERE ITEM_ID =\ %d', DB_TBL_PREFIX, result = mysql_query(GLOBALS['DB']); result); echo '<tr>'; echo '<td><a href="shop.php?item=' . row['ITEM_NAME'] . '</a></td>'; echo '<td><select name="qty[' . i=0; i++) { echo '<option '; if (qty) { echo 'selected="selected" '; } echo 'value="' . i . '</option>'; } echo '</td>'; echo '<td>row['PRICE'], 2) . '</td>'; echo '<td>row['PRICE'] * </td>'; echo '</tr>'; total += qty; mysql_free_result(</table>'; echo '<input type="submit" value="Update"/>'; echo '<p>Total Items: ' . cart->totalItems . '<br/>'; echo 'Total Quantity: ' . </p>'; echo '<p><b>Total Price: ' . number_format(</b></p>'; // display link to checkout echo '<p><a href=<checkout.php>>'; echo '<img src="img/checkout.gif" alt="Proceed to Checkout"/></a></p>'; } // save cart to session and display the page _SESSION['cart'] = serialize(GLOBALS['TEMPLATE']['content'] = ob_get_clean(); include '../templates/template-page.php'; } ?>
(C) Æliens 20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.