V5
This commit is contained in:
parent
d22c607918
commit
eb2cf1a3fb
@ -1,56 +1,52 @@
|
||||
<?php
|
||||
session_start();
|
||||
|
||||
require_once 'db/config.php';
|
||||
|
||||
// Initialize cart if it doesn't exist
|
||||
if (!isset($_SESSION['cart'])) {
|
||||
$_SESSION['cart'] = [];
|
||||
$_SESSION['cart_restaurant'] = null;
|
||||
// Check if user is logged in, if not, redirect to login page
|
||||
if (!isset($_SESSION['user_id'])) {
|
||||
// For now, we'll use a hardcoded user_id for simplicity.
|
||||
// In a real application, you would redirect to a login page.
|
||||
$_SESSION['user_id'] = 1; // Hardcoded user_id for demonstration
|
||||
}
|
||||
|
||||
$action = $_POST['action'] ?? $_GET['action'] ?? null;
|
||||
$menu_item_id = $_POST['menu_item_id'] ?? $_GET['menu_item_id'] ?? null;
|
||||
$restaurant_id = $_POST['restaurant_id'] ?? null;
|
||||
$quantity = $_POST['quantity'] ?? 1;
|
||||
$user_id = $_SESSION['user_id'];
|
||||
$action = $_POST['action'] ?? '';
|
||||
|
||||
switch ($action) {
|
||||
case 'add':
|
||||
if ($menu_item_id && $restaurant_id && $quantity > 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();
|
||||
|
||||
case 'clear':
|
||||
$_SESSION['cart'] = [];
|
||||
$_SESSION['cart_restaurant'] = null;
|
||||
header('Location: cart.php');
|
||||
exit;
|
||||
// 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();
|
||||
|
||||
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.']);
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
159
menu.php
159
menu.php
@ -1,85 +1,122 @@
|
||||
<?php
|
||||
require_once 'db/config.php';
|
||||
|
||||
$restaurant_id = $_GET['id'] ?? null;
|
||||
$restaurant = null;
|
||||
$menu_items = [];
|
||||
// Get restaurant ID from the URL
|
||||
$restaurant_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
|
||||
|
||||
if ($restaurant_id) {
|
||||
$stmt = db()->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';
|
||||
?>
|
||||
|
||||
<main>
|
||||
<div class="container">
|
||||
<?php if ($restaurant): ?>
|
||||
<section class="restaurant-hero-menu" style="background-image: url('<?= htmlspecialchars($restaurant['image_url']) ?>');">
|
||||
<div class="restaurant-hero-menu-content">
|
||||
<h1><?= htmlspecialchars($restaurant['name']) ?></h1>
|
||||
<p><?= htmlspecialchars($restaurant['cuisine']) ?></p>
|
||||
<section class="restaurant-hero" style="background-image: url('<?= htmlspecialchars($restaurant['image_url']) ?>');">
|
||||
<div class="restaurant-hero-content">
|
||||
<h1><?= htmlspecialchars($restaurant['name']) ?></h1>
|
||||
<p><?= htmlspecialchars($restaurant['cuisine']) ?></p>
|
||||
<?php if (isset($restaurant['rating']) && $restaurant['rating'] > 0): ?>
|
||||
<div class="rating-display">
|
||||
<span class="star">★</span>
|
||||
<span><?= htmlspecialchars(number_format($restaurant['rating'], 1)) ?></span>
|
||||
<span class="rating-count">(<?= htmlspecialchars($restaurant['rating_count']) ?> ratings)</span>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<div class="menu-search-container">
|
||||
<input type="text" id="menu-search-input" class="search-bar" placeholder="Search menu items...">
|
||||
<?php else: ?>
|
||||
<div class="rating-display"><span class="rating-count">No ratings yet</span></div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="menu-grid" id="menu-grid">
|
||||
<?php foreach ($menu_items as $item): ?>
|
||||
<div class="menu-item-card" data-name="<?= htmlspecialchars(strtolower($item['name'])) ?>" data-description="<?= htmlspecialchars(strtolower($item['description'])) ?>">
|
||||
<img src="<?= htmlspecialchars($item['image_url']) ?>" alt="<?= htmlspecialchars($item['name']) ?>">
|
||||
<div class="menu-item-card-content">
|
||||
<h3><?= htmlspecialchars($item['name']) ?></h3>
|
||||
<p class="description"><?= htmlspecialchars($item['description']) ?></p>
|
||||
<p class="price">$<?= htmlspecialchars(number_format($item['price'], 2)) ?></p>
|
||||
<form action="cart_actions.php" method="POST" class="add-to-cart-form">
|
||||
<input type="hidden" name="action" value="add">
|
||||
<input type="hidden" name="menu_item_id" value="<?= $item['id'] ?>">
|
||||
<input type="hidden" name="restaurant_id" value="<?= $restaurant['id'] ?>">
|
||||
<div class="form-row">
|
||||
<input type="number" name="quantity" value="1" min="1" class="quantity-input">
|
||||
<button type="submit" class="add-to-cart-btn">Add to Cart</button>
|
||||
</div>
|
||||
</form>
|
||||
<section class="restaurant-menu">
|
||||
<h2>Menu</h2>
|
||||
<div class="menu-grid">
|
||||
<?php if (empty($menu_items)): ?>
|
||||
<p>No menu items available for this restaurant.</p>
|
||||
<?php else: ?>
|
||||
<?php foreach ($menu_items as $item): ?>
|
||||
<div class="menu-item-card">
|
||||
<img src="<?= htmlspecialchars($item['image_url']) ?>" alt="<?= htmlspecialchars($item['name']) ?>">
|
||||
<div class="menu-item-card-content">
|
||||
<h3><?= htmlspecialchars($item['name']) ?></h3>
|
||||
<p class="description"><?= htmlspecialchars($item['description']) ?></p>
|
||||
<p class="price">$<?= htmlspecialchars(number_format($item['price'], 2)) ?></p>
|
||||
<form class="add-to-cart-form">
|
||||
<input type="hidden" name="action" value="add">
|
||||
<input type="hidden" name="menu_item_id" value="<?= $item['id'] ?>">
|
||||
<div class="form-row">
|
||||
<input type="number" name="quantity" value="1" min="1" class="quantity-input">
|
||||
<button type="submit" class="add-to-cart-btn">Add to Cart</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
</section>
|
||||
|
||||
<section class="rating-form-container">
|
||||
<h2>Rate your experience</h2>
|
||||
<form action="rate.php" method="POST" class="rate-form">
|
||||
<input type="hidden" name="restaurant_id" value="<?= $restaurant['id'] ?>">
|
||||
<div class="stars">
|
||||
<input type="radio" id="star5" name="rating" value="5"><label for="star5">★</label>
|
||||
<input type="radio" id="star4" name="rating" value="4"><label for="star4">★</label>
|
||||
<input type="radio" id="star3" name="rating" value="3"><label for="star3">★</label>
|
||||
<input type="radio" id="star2" name="rating" value="2"><label for="star2">★</label>
|
||||
<input type="radio" id="star1" name="rating" value="1"><label for="star1">★</label>
|
||||
</div>
|
||||
<button type="submit">Submit Rating</button>
|
||||
</form>
|
||||
</section>
|
||||
|
||||
<?php else: ?>
|
||||
<p>Restaurant not found.</p>
|
||||
<?php endif; ?>
|
||||
<?php endforeach; ?>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<?php include 'footer.php'; // Assuming you might create a footer file ?>
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
const forms = document.querySelectorAll('.add-to-cart-form');
|
||||
forms.forEach(form => {
|
||||
form.addEventListener('submit', function (e) {
|
||||
e.preventDefault();
|
||||
|
||||
const formData = new FormData(this);
|
||||
const menuItemId = formData.get('menu_item_id');
|
||||
const quantity = formData.get('quantity');
|
||||
|
||||
// Since we don't have user authentication, we'll use a hardcoded user_id.
|
||||
const userId = 1;
|
||||
|
||||
const data = new FormData();
|
||||
data.append('action', 'add');
|
||||
data.append('menu_item_id', menuItemId);
|
||||
data.append('quantity', quantity);
|
||||
data.append('user_id', userId);
|
||||
|
||||
fetch('cart_actions.php', {
|
||||
method: 'POST',
|
||||
body: data
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(result => {
|
||||
if (result.success) {
|
||||
alert('Item added to cart!');
|
||||
} else {
|
||||
alert('Error: ' + (result.error || 'Could not add item to cart.'));
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error);
|
||||
alert('An unexpected error occurred.');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
<?php include 'footer.php'; ?>
|
||||
Loading…
x
Reference in New Issue
Block a user