34968-vm/menu.php
Flatlogic Bot a6434721b7 V5
2025-10-15 00:36:03 +00:00

128 lines
5.0 KiB
PHP

<?php
require_once 'db/config.php';
// Get restaurant ID from the URL
$restaurant_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if (!$restaurant_id) {
header('Location: index.php');
exit();
}
$pdo = db();
// Fetch restaurant details along with average rating
$restaurant_stmt = $pdo->prepare("
SELECT r.*, AVG(rt.rating) as rating, COUNT(rt.id) as rating_count
FROM restaurants r
LEFT JOIN ratings rt ON r.id = rt.restaurant_id
WHERE r.id = ?
GROUP BY r.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">
<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>
<?php else: ?>
<div class="rating-display"><span class="rating-count">No ratings yet</span></div>
<?php endif; ?>
</div>
</section>
<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>
<?php endforeach; ?>
<?php endif; ?>
</div>
</section>
</div>
</main>
<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'; ?>