diff --git a/cart_actions.php b/cart_actions.php index ddc12c1a..e9e64951 100644 --- a/cart_actions.php +++ b/cart_actions.php @@ -1,56 +1,52 @@ 0) { - // If cart is not empty and new item is from a different restaurant, clear the cart - if (!empty($_SESSION['cart']) && $_SESSION['cart_restaurant'] != $restaurant_id) { - $_SESSION['cart'] = []; - } +if ($_SERVER['REQUEST_METHOD'] === 'POST') { + if ($action === 'add') { + $menu_item_id = $_POST['menu_item_id'] ?? null; + $quantity = $_POST['quantity'] ?? 1; - $_SESSION['cart_restaurant'] = $restaurant_id; - - // Add or update item in cart - if (isset($_SESSION['cart'][$menu_item_id])) { - $_SESSION['cart'][$menu_item_id] += $quantity; - } else { - $_SESSION['cart'][$menu_item_id] = $quantity; - } - } - header('Location: menu.php?id=' . $restaurant_id); - exit; - - case 'update': - if ($menu_item_id && $quantity > 0) { - $_SESSION['cart'][$menu_item_id] = $quantity; - } - header('Location: cart.php'); - exit; - - case 'remove': if ($menu_item_id) { - unset($_SESSION['cart'][$menu_item_id]); - } - header('Location: cart.php'); - exit; + try { + $pdo = db(); + + // Check if the item is already in the cart + $stmt = $pdo->prepare("SELECT * FROM cart WHERE user_id = ? AND menu_item_id = ?"); + $stmt->execute([$user_id, $menu_item_id]); + $existing_item = $stmt->fetch(); - case 'clear': - $_SESSION['cart'] = []; - $_SESSION['cart_restaurant'] = null; - header('Location: cart.php'); - exit; -} \ No newline at end of file + if ($existing_item) { + // If item exists, update the quantity + $new_quantity = $existing_item['quantity'] + $quantity; + $update_stmt = $pdo->prepare("UPDATE cart SET quantity = ? WHERE id = ?"); + $update_stmt->execute([$new_quantity, $existing_item['id']]); + } else { + // If item does not exist, insert it + $insert_stmt = $pdo->prepare("INSERT INTO cart (user_id, menu_item_id, quantity) VALUES (?, ?, ?)"); + $insert_stmt->execute([$user_id, $menu_item_id, $quantity]); + } + + echo json_encode(['success' => true, 'message' => 'Item added to cart.']); + + } catch (PDOException $e) { + http_response_code(500); + echo json_encode(['success' => false, 'message' => 'Database error: ' . $e->getMessage()]); + } + } else { + http_response_code(400); + echo json_encode(['success' => false, 'message' => 'Menu item ID is required.']); + } + } +} +?> \ No newline at end of file diff --git a/menu.php b/menu.php index 25c67b07..7df8d62a 100644 --- a/menu.php +++ b/menu.php @@ -1,85 +1,122 @@ prepare("SELECT * FROM restaurants WHERE id = ?"); - $stmt->execute([$restaurant_id]); - $restaurant = $stmt->fetch(PDO::FETCH_ASSOC); +if (!$restaurant_id) { + header('Location: index.php'); + exit(); } -if ($restaurant) { - $stmt = db()->prepare("SELECT * FROM menu_items WHERE restaurant_id = ?"); - $stmt->execute([$restaurant_id]); - $menu_items = $stmt->fetchAll(PDO::FETCH_ASSOC); +$pdo = db(); + +// Fetch restaurant details +$restaurant_stmt = $pdo->prepare("SELECT * FROM restaurants WHERE id = ?"); +$restaurant_stmt->execute([$restaurant_id]); +$restaurant = $restaurant_stmt->fetch(PDO::FETCH_ASSOC); + +// If restaurant not found, redirect +if (!$restaurant) { + header('Location: index.php'); + exit(); } +// Fetch menu items for the specific restaurant +$menu_items_stmt = $pdo->prepare("SELECT * FROM menu_items WHERE restaurant_id = ? ORDER BY name"); +$menu_items_stmt->execute([$restaurant_id]); +$menu_items = $menu_items_stmt->fetchAll(PDO::FETCH_ASSOC); + include 'header.php'; ?>
- -
-
-

-

+
+
+

+

+ 0): ?>
( ratings)
-
-
- - +
-
- \ No newline at end of file + + + \ No newline at end of file