V5
This commit is contained in:
parent
d22c607918
commit
eb2cf1a3fb
@ -1,56 +1,52 @@
|
|||||||
<?php
|
<?php
|
||||||
session_start();
|
session_start();
|
||||||
|
|
||||||
require_once 'db/config.php';
|
require_once 'db/config.php';
|
||||||
|
|
||||||
// Initialize cart if it doesn't exist
|
// Check if user is logged in, if not, redirect to login page
|
||||||
if (!isset($_SESSION['cart'])) {
|
if (!isset($_SESSION['user_id'])) {
|
||||||
$_SESSION['cart'] = [];
|
// For now, we'll use a hardcoded user_id for simplicity.
|
||||||
$_SESSION['cart_restaurant'] = null;
|
// 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;
|
$user_id = $_SESSION['user_id'];
|
||||||
$menu_item_id = $_POST['menu_item_id'] ?? $_GET['menu_item_id'] ?? null;
|
$action = $_POST['action'] ?? '';
|
||||||
$restaurant_id = $_POST['restaurant_id'] ?? null;
|
|
||||||
$quantity = $_POST['quantity'] ?? 1;
|
|
||||||
|
|
||||||
switch ($action) {
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||||
case 'add':
|
if ($action === 'add') {
|
||||||
if ($menu_item_id && $restaurant_id && $quantity > 0) {
|
$menu_item_id = $_POST['menu_item_id'] ?? null;
|
||||||
// If cart is not empty and new item is from a different restaurant, clear the cart
|
$quantity = $_POST['quantity'] ?? 1;
|
||||||
if (!empty($_SESSION['cart']) && $_SESSION['cart_restaurant'] != $restaurant_id) {
|
|
||||||
$_SESSION['cart'] = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
$_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) {
|
if ($menu_item_id) {
|
||||||
unset($_SESSION['cart'][$menu_item_id]);
|
try {
|
||||||
}
|
$pdo = db();
|
||||||
header('Location: cart.php');
|
|
||||||
exit;
|
|
||||||
|
|
||||||
case 'clear':
|
// Check if the item is already in the cart
|
||||||
$_SESSION['cart'] = [];
|
$stmt = $pdo->prepare("SELECT * FROM cart WHERE user_id = ? AND menu_item_id = ?");
|
||||||
$_SESSION['cart_restaurant'] = null;
|
$stmt->execute([$user_id, $menu_item_id]);
|
||||||
header('Location: cart.php');
|
$existing_item = $stmt->fetch();
|
||||||
exit;
|
|
||||||
|
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
|
<?php
|
||||||
require_once 'db/config.php';
|
require_once 'db/config.php';
|
||||||
|
|
||||||
$restaurant_id = $_GET['id'] ?? null;
|
// Get restaurant ID from the URL
|
||||||
$restaurant = null;
|
$restaurant_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
|
||||||
$menu_items = [];
|
|
||||||
|
|
||||||
if ($restaurant_id) {
|
if (!$restaurant_id) {
|
||||||
$stmt = db()->prepare("SELECT * FROM restaurants WHERE id = ?");
|
header('Location: index.php');
|
||||||
$stmt->execute([$restaurant_id]);
|
exit();
|
||||||
$restaurant = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($restaurant) {
|
$pdo = db();
|
||||||
$stmt = db()->prepare("SELECT * FROM menu_items WHERE restaurant_id = ?");
|
|
||||||
$stmt->execute([$restaurant_id]);
|
// Fetch restaurant details
|
||||||
$menu_items = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
$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';
|
include 'header.php';
|
||||||
?>
|
?>
|
||||||
|
|
||||||
<main>
|
<main>
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<?php if ($restaurant): ?>
|
<section class="restaurant-hero" style="background-image: url('<?= htmlspecialchars($restaurant['image_url']) ?>');">
|
||||||
<section class="restaurant-hero-menu" style="background-image: url('<?= htmlspecialchars($restaurant['image_url']) ?>');">
|
<div class="restaurant-hero-content">
|
||||||
<div class="restaurant-hero-menu-content">
|
<h1><?= htmlspecialchars($restaurant['name']) ?></h1>
|
||||||
<h1><?= htmlspecialchars($restaurant['name']) ?></h1>
|
<p><?= htmlspecialchars($restaurant['cuisine']) ?></p>
|
||||||
<p><?= htmlspecialchars($restaurant['cuisine']) ?></p>
|
<?php if (isset($restaurant['rating']) && $restaurant['rating'] > 0): ?>
|
||||||
<div class="rating-display">
|
<div class="rating-display">
|
||||||
<span class="star">★</span>
|
<span class="star">★</span>
|
||||||
<span><?= htmlspecialchars(number_format($restaurant['rating'], 1)) ?></span>
|
<span><?= htmlspecialchars(number_format($restaurant['rating'], 1)) ?></span>
|
||||||
<span class="rating-count">(<?= htmlspecialchars($restaurant['rating_count']) ?> ratings)</span>
|
<span class="rating-count">(<?= htmlspecialchars($restaurant['rating_count']) ?> ratings)</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
<?php else: ?>
|
||||||
</section>
|
<div class="rating-display"><span class="rating-count">No ratings yet</span></div>
|
||||||
|
<?php endif; ?>
|
||||||
<div class="menu-search-container">
|
|
||||||
<input type="text" id="menu-search-input" class="search-bar" placeholder="Search menu items...">
|
|
||||||
</div>
|
</div>
|
||||||
|
</section>
|
||||||
|
|
||||||
<section class="menu-grid" id="menu-grid">
|
<section class="restaurant-menu">
|
||||||
<?php foreach ($menu_items as $item): ?>
|
<h2>Menu</h2>
|
||||||
<div class="menu-item-card" data-name="<?= htmlspecialchars(strtolower($item['name'])) ?>" data-description="<?= htmlspecialchars(strtolower($item['description'])) ?>">
|
<div class="menu-grid">
|
||||||
<img src="<?= htmlspecialchars($item['image_url']) ?>" alt="<?= htmlspecialchars($item['name']) ?>">
|
<?php if (empty($menu_items)): ?>
|
||||||
<div class="menu-item-card-content">
|
<p>No menu items available for this restaurant.</p>
|
||||||
<h3><?= htmlspecialchars($item['name']) ?></h3>
|
<?php else: ?>
|
||||||
<p class="description"><?= htmlspecialchars($item['description']) ?></p>
|
<?php foreach ($menu_items as $item): ?>
|
||||||
<p class="price">$<?= htmlspecialchars(number_format($item['price'], 2)) ?></p>
|
<div class="menu-item-card">
|
||||||
<form action="cart_actions.php" method="POST" class="add-to-cart-form">
|
<img src="<?= htmlspecialchars($item['image_url']) ?>" alt="<?= htmlspecialchars($item['name']) ?>">
|
||||||
<input type="hidden" name="action" value="add">
|
<div class="menu-item-card-content">
|
||||||
<input type="hidden" name="menu_item_id" value="<?= $item['id'] ?>">
|
<h3><?= htmlspecialchars($item['name']) ?></h3>
|
||||||
<input type="hidden" name="restaurant_id" value="<?= $restaurant['id'] ?>">
|
<p class="description"><?= htmlspecialchars($item['description']) ?></p>
|
||||||
<div class="form-row">
|
<p class="price">$<?= htmlspecialchars(number_format($item['price'], 2)) ?></p>
|
||||||
<input type="number" name="quantity" value="1" min="1" class="quantity-input">
|
<form class="add-to-cart-form">
|
||||||
<button type="submit" class="add-to-cart-btn">Add to Cart</button>
|
<input type="hidden" name="action" value="add">
|
||||||
</div>
|
<input type="hidden" name="menu_item_id" value="<?= $item['id'] ?>">
|
||||||
</form>
|
<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>
|
||||||
</div>
|
<?php endforeach; ?>
|
||||||
<?php endforeach; ?>
|
<?php endif; ?>
|
||||||
</section>
|
</div>
|
||||||
|
</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; ?>
|
|
||||||
</div>
|
</div>
|
||||||
</main>
|
</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