topical media & game development
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(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 = _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(_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 ' . result);
}
echo '</p>';
if (row['CATEGORY_ID']))
{
echo '&category=' . 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'] * total += qty;
mysql_free_result(cart->totalItems . '<br/>';
echo 'Total Quantity: ' . ' . number_format(_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.