topical media & game development
professional-sql-08-public-files-shop.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();
}
// display sales page for a particular item
if (isset(query = sprintf('
SELECT
ITEM_ID, ITEM_NAME, ITEM_DESCRIPTION, PRICE, ITEM_IMAGE,
C.CATEGORY_ID, CATEGORY_NAME
FROM
\%sSHOP_INVENTORY I
JOIN \%sSHOP_CATEGORY C ON I.CATEGORY_ID = C.CATEGORY_ID
WHERE
ITEM_ID = \%d',
DB_TBL_PREFIX,
DB_TBL_PREFIX,
result = mysql_query(GLOBALS['DB']);
// item does not exist so redirect to main categories list
if (!mysql_num_rows(result);
header('Location: shop.php');
exit();
}
result);
ob_start();
echo '<p><a href="cart.php?category=' . row['ITEM_NAME'] . '</h1>';
echo '<p><a href=<shop.php>>Back to all categories</a> / ';
echo '<a href="shop.php?category=' . row['CATEGORY_NAME'] . '</a></p>';
echo '<table>';
echo '<tr><td rowspan="3">';
echo '<img src="' . row['ITEM_NAME'] . '</b></td></tr>';
echo '<tr><td>' . nl2br(' . number_format(cart->qtyItem(row['ITEM_ID'] . '">';
echo '<img src="img/cartadd.gif" alt="Add to Cart"/></a>';
}
else
{
echo '<a href="cart.php?remove&item=' . GLOBALS['TEMPLATE']['content'] = ob_get_clean();
}
// display list of items in category
else if (isset(query = sprintf('SELECT CATEGORY_ID, CATEGORY_NAME FROM ' .
'\%sSHOP_CATEGORY WHERE CATEGORY_ID = \%d',
DB_TBL_PREFIX,
result = mysql_query(GLOBALS['DB']);
// category does not exist so redirect to main categories list
if (!mysql_num_rows(result);
header('Location: shop.php');
exit();
}
result);
row['CATEGORY_ID'];
row['CATEGORY_NAME'];
mysql_free_result(id . '">';
echo '<img src="img/cartview.gif" alt="View Cart"/></a></p>';
echo '<h1>Products in ' . query = sprintf('SELECT ITEM_ID, ITEM_NAME, ITEM_IMAGE ' .
'FROM \%sSHOP_INVENTORY WHERE CATEGORY_ID = \%d ORDER BY ITEM_NAME ASC',
DB_TBL_PREFIX,
result = mysql_query(GLOBALS['DB']);
while (result))
{
echo '<table>';
echo '<tr><td><img src="' . row['ITEM_ID'] . '">' .
GLOBALS['TEMPLATE']['content'] = ob_get_clean();
}
// display main list of categories and the number of products within each
else
{
ob_start();
echo '<p><a href=<cart.php>>' .
'<img src="img/cartview.gif" alt="View Cart"/></a></p>';
echo '<h1>All Categories</h1>';
// Note: LEFT JOIN not specified so any categories without products will
// not be included in the results
result = mysql_query(GLOBALS['DB']);
echo '<ul>';
while (result))
{
printf('<li><a href="shop.php?category=\%d">\%s</a> (\%d \%s)</li>',
row['CATEGORY_NAME'],
row['ITEM_COUNT'] == 1) ? 'product' : 'products'));
}
mysql_free_result(GLOBALS['TEMPLATE']['content'] = ob_get_clean();
}
// display the page
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.