コード例 #1
0
ファイル: add_cart.php プロジェクト: arvin-tcm/CS526_Web
 */
$addCartSubmitted = isset($_POST['addcart_submitted']);
if ($addCartSubmitted) {
    $food_id = $_POST['food_id'];
    $quantity = $_POST['quantity'];
    // data validate
    if (empty($quantity) || $quantity <= 0) {
        $error = "Invalid food quantity";
        include '../errors/error.php';
    } else {
        $index = false;
        for ($i = 0; $i < sizeof($_SESSION['cart']); $i++) {
            if ($_SESSION['cart'][$i]->food_id === $food_id) {
                $index = $i;
                break;
            }
        }
        if ($index) {
            $_SESSON['cart'][$index]->quantity += $quantity;
        } else {
            $food = FoodRepository::getfood($food_id);
            $newItem = new stdClass();
            $newItem->food_id = $food_id;
            $newItem->quantity = $quantity;
            $newItem->food = $food;
            array_push($_SESSION['cart'], $newItem);
        }
        header("Location: .?controller=guest&action=view_food&food_id={$food_id}");
    }
} else {
}