<?php

require_once "../includes/LIB-project1.php";
require_once "../includes/Paginator.class.php";
require_once "../includes/Database.class.php";
$css = array("main.css", "bootstrap.min.css");
$curr_page = "Cart";
//set up the framework for the html header
$dbObj = new Database();
//if set, clear the cart database table and unset variable for future use
if (isset($_GET['clearCart'])) {
    clearCart($dbObj);
    unset($_GET['clearCart']);
    //unset it so it'll pick up future ones
}
//save page HTML
$pageHTML = "<div class='container'>";
$pageHTML .= displayCart($dbObj);
$pageHTML .= "</div>";
include "../includes/HTML_template.php";
//close connection
$dbObj->closeDbh();
function authenticateCartPOST()
{
    @session_start();
    /*if(isset($_POST['dosage'])){
    			echo "<h1>Dosage post is set!"; 
    			echo "<h1>Dosage 0 : ".$_POST['dosage'][0]."</h1>"; 
    		}*/
    $nCart = count($_SESSION['cart']);
    $db = connect(0, 0, 0);
    $availability = array();
    $valid = array();
    $valide = array();
    $error = array();
    if (!mysqli_connect_errno()) {
        for ($i = 0; $i < $nCart; $i++) {
            $_SESSION['qty'][$i] = $_POST['dosage'][$i];
            //echo "<p>".$_SESSION['cart']['qty'][$i]."</p>";
            $dosage_query = "select * from DOSAGE natural join DOSAGE_DETAILS natural join COMPOUND natural join STORE where dosage_id = " . $_SESSION['cart'][$i];
            $result = $db->query($dosage_query);
            $row = $result->fetch_assoc();
            $quantity = $row['quantity'];
            $value = $row['value'];
            $remaining = $row['remaining'];
            mysqli_free_result($result);
            if ($_POST['dosage'][$i] * $quantity * $value > $remaining) {
                $availability[$i] = "There is not enough of this item left in stock!";
                $valid[$i] = false;
            } else {
                $availability[$i] = "";
                $valid[$i] = true;
            }
            if (empty($_POST['dosage'][$i])) {
                $error[$i] = "Please select a quantity for you order!";
                $valide[$i] = false;
            } else {
                $valide[$i] = true;
            }
        }
    }
    $valid_final = true;
    for ($j = 0; $j < count($valid); $j++) {
        if ($valid == false) {
            $valid_final = false;
            break;
        } else {
            $valid_final = true;
        }
        if ($valide == false) {
            $valid_final = false;
            break;
        } else {
            $valid_final = true;
        }
    }
    if ($valid_final) {
        confirmCart();
    } else {
        displayCart();
        for ($d = 0; $d < count($availability); $d++) {
            echo "<label class='formerror'> Line item " . $d . " : " . $availability[$d] . "</label><br>";
        }
    }
}
    }
    public function getPrice()
    {
        return $this->price;
    }
}
$products = array(1 => new Product(1, "SuperWidget", 19.99), 2 => new Product(2, "MegaWidget", 29.99), 3 => new Product(3, "WonderWidget", 39.99));
if (!isset($_SESSION["cart"])) {
    $_SESSION["cart"] = array();
}
if (isset($_GET["action"]) and $_GET["action"] == "addItem") {
    addItem();
} elseif (isset($_GET["action"]) and $_GET["action"] == "removeItem") {
    removeItem();
} else {
    displayCart();
}
function addItem()
{
    global $products;
    if (isset($_GET["productId"]) and $_GET["productId"] >= 1 and $_GET["productId"] <= 3) {
        $productId = (int) $_GET["productId"];
        if (!isset($_SESSION["cart"][$productId])) {
            $_SESSION["cart"][$productId] = $products[$productId];
        }
    }
    session_write_close();
    header("Location: shopping_cart.php");
}
function removeItem()
{
Beispiel #4
0
            $cartCount += $row["qty"];
            $lineTotal = $row["price"] * $row["qty"];
            $cartAmount += $lineTotal;
            $template->setCurrentBlock("item");
            $template->setVariable("QUANTITY_NAME", $row["item_id"]);
            $template->setVariable("QUANTITY_VALUE", $row["qty"]);
            $template->setVariable("WINE", showWine($row["wine_id"], $connection));
            $template->setVariable("ITEM_PRICE", sprintf("%-.2f", $row["price"]));
            $template->setVariable("TOTAL_VALUE", sprintf("%-.2f", $lineTotal));
            $template->parseCurrentBlock("item");
        }
        $template->setCurrentBlock("cart");
        $template->setVariable("TOTAL_ITEMS", $cartCount);
        $template->setVariable("TOTAL_COST", sprintf("%-.2f", $cartAmount));
        $template->parseCurrentBlock("cart");
    } else {
        // The user has not put anything in the cart
        $template->setCurrentBlock("emptycart");
        $template->setVariable("TEXT", "Your cart is empty");
        $template->parseCurrentBlock("emptycart");
    }
}
session_start();
$template = new winestoreTemplate(T_SHOWCART);
$connection = DB::connect($dsn, true);
if (DB::isError($connection)) {
    trigger_error($connection->getMessage(), E_USER_ERROR);
}
// Show the contents of the shopping cart
displayCart($connection, $template);
$template->showWinestore(SHOW_ALL, B_ALL & ~B_SHOW_CART & ~B_PASSWORD & ~B_DETAILS);