Example #1
0
$page = intval($page);
$browseNode = intval($browseNode);
// wtf??? no reference for safeString function
$search = safeString($search);
if (!isset($_SESSION['cart'])) {
    session_register('cart');
    $_SESSION['cart'] = [];
}
// instead of three if statements I used switch
// more convenient way
switch ($action) {
    case 'addtocart':
        addToCart($_SESSION['cart'], $ASIN, $mode);
        break;
    case 'deletefromcart':
        deleteFromCart($_SESSION['cart'], $ASIN);
        break;
    case 'emptycart':
        $_SESSION['cart'] = [];
        break;
}
// Page is generating here
// caption generation
require_once 'topbar.php';
// Main part
// or body of the page depends of action which come
// from probably GET or POST
// category list will be shown in most cases
// default action is browsing default category
// default category is defined in constants.php
switch ($action) {
function updateCart()
{
    $cartId = $_POST['hidCartId'];
    $productId = $_POST['hidProductId'];
    $itemQty = $_POST['txtQty'];
    $numItem = count($itemQty);
    $numDeleted = 0;
    $notice = '';
    $i = 0;
    for ($i = 0; $i < $numItem; $i++) {
        $newQty = (int) $itemQty[$i];
        if ($newQty < 1) {
            // remove this item from shopping cart
            deleteFromCart($cartId[$i]);
            $numDeleted += 1;
        } else {
            // check current stock
            $sql = "SELECT pd_name, pd_qty\n\t\t\t        FROM tbl_product \n\t\t\t\t\tWHERE pd_id = {$productId[$i]}";
            $result = dbQuery($sql);
            $row = dbFetchAssoc($result);
            if ($newQty > $row['pd_qty']) {
                // we only have this much in stock
                $newQty = $row['pd_qty'];
                // if the customer put more than
                // we have in stock, give a notice
                if ($row['pd_qty'] > 0) {
                    setError('The quantity you have requested is more than we currently have in stock. The number available is indicated in the &quot;Quantity&quot; box. ');
                } else {
                    // the product is no longer in stock
                    setError('Sorry, but the product you want (' . $row['pd_name'] . ') is no longer in stock');
                    // remove this item from shopping cart
                    deleteFromCart($cartId[$i]);
                    $numDeleted += 1;
                }
            }
            // update product quantity
            $sql = "UPDATE tbl_cart\n\t\t\t\t\tSET ct_qty = {$newQty}\n\t\t\t\t\tWHERE ct_id = {$cartId[$i]}";
            dbQuery($sql);
        }
    }
    if ($numDeleted == $numItem) {
        // if all item deleted return to the last page that
        // the customer visited before going to shopping cart
        header("Location: {$returnUrl}" . $_SESSION['shop_return_url']);
    } else {
        header('Location: cart.php');
    }
    exit;
}
</div>


<?php 
require_once 'library/config.php';
require_once 'library/cart-functions.php';
$action = isset($_GET['action']) && $_GET['action'] != '' ? $_GET['action'] : 'view';
switch ($action) {
    case 'add':
        addToCart();
        break;
    case 'update':
        updateCart();
        break;
    case 'delete':
        deleteFromCart();
        break;
    case 'view':
}
$cartContent = getCartContent();
$numItem = count($cartContent);
$pageTitle = 'Shopping Cart';
// show the error message ( if we have any )
displayError();
if ($numItem > 0) {
    ?>

 <table width="780" border="0" align="center" cellpadding="5" cellspacing="1" class="entryTable">
  <tr id="entryTableHeader"> 
   <td colspan="2" align="center">Item</td>
   <td width="229" align="center">Unit Price</td>
// pages and browseNodes must be integers
$browseNode = intval($browseNode);
// it may cause some confusion, but we are stripping characters out from
// $search it seems only fair to modify it now so it will be displayed
// in the heading
$search = safeString($search);
if (!isset($HTTP_SESSION_VARS['cart'])) {
    session_register('cart');
    $HTTP_SESSION_VARS['cart'] = array();
}
// tasks that need to be done before the top bar is shown
if ($action == 'addtocart') {
    addToCart($HTTP_SESSION_VARS['cart'], $ASIN, $mode);
}
if ($action == 'deletefromcart') {
    deleteFromCart($HTTP_SESSION_VARS['cart'], $ASIN);
}
if ($action == 'emptycart') {
    $HTTP_SESSION_VARS['cart'] = array();
}
// show top bar
require_once 'topbar.php';
// main event loop.  Reacts to user action on the calling page
switch ($action) {
    case 'detail':
        showCategories($mode);
        showDetail($ASIN, $mode);
        break;
    case 'addtocart':
    case 'deletefromcart':
    case 'emptycart':