V10
This commit is contained in:
parent
727b6fcf29
commit
c3d7232b7a
@ -8,19 +8,42 @@ if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== tru
|
||||
exit;
|
||||
}
|
||||
|
||||
$pdo = db();
|
||||
|
||||
// Fetch all cuisines
|
||||
$cuisines_stmt = $pdo->query("SELECT * FROM cuisines ORDER BY name ASC");
|
||||
$cuisines = $cuisines_stmt->fetchAll();
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$name = $_POST['name'] ?? '';
|
||||
$cuisine = $_POST['cuisine'] ?? '';
|
||||
$description = $_POST['description'] ?? '';
|
||||
$image_url = $_POST['image_url'] ?? '';
|
||||
$selected_cuisines = $_POST['cuisines'] ?? [];
|
||||
|
||||
if ($name && $cuisine) {
|
||||
$pdo = db();
|
||||
$stmt = $pdo->prepare("INSERT INTO restaurants (name, cuisine, image_url) VALUES (?, ?, ?)");
|
||||
$stmt->execute([$name, $cuisine, $image_url]);
|
||||
header('Location: index.php');
|
||||
exit;
|
||||
if ($name && !empty($selected_cuisines)) {
|
||||
try {
|
||||
$pdo->beginTransaction();
|
||||
|
||||
// Insert into restaurants table
|
||||
$stmt = $pdo->prepare("INSERT INTO restaurants (name, description, image_url) VALUES (?, ?, ?)");
|
||||
$stmt->execute([$name, $description, $image_url]);
|
||||
$restaurant_id = $pdo->lastInsertId();
|
||||
|
||||
// Insert into restaurant_cuisines table
|
||||
$cuisine_stmt = $pdo->prepare("INSERT INTO restaurant_cuisines (restaurant_id, cuisine_id) VALUES (?, ?)");
|
||||
foreach ($selected_cuisines as $cuisine_id) {
|
||||
$cuisine_stmt->execute([$restaurant_id, $cuisine_id]);
|
||||
}
|
||||
|
||||
$pdo->commit();
|
||||
header('Location: index.php');
|
||||
exit;
|
||||
} catch (Exception $e) {
|
||||
$pdo->rollBack();
|
||||
$error = "Error adding restaurant: " . $e->getMessage();
|
||||
}
|
||||
} else {
|
||||
$error = "Name and cuisine are required.";
|
||||
$error = "Name and at least one cuisine are required.";
|
||||
}
|
||||
}
|
||||
?>
|
||||
@ -38,8 +61,23 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
<input type="text" class="form-control" id="name" name="name" required>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="cuisine" class="form-label">Cuisine</label>
|
||||
<input type="text" class="form-control" id="cuisine" name="cuisine" required>
|
||||
<label for="description" class="form-label">Description</label>
|
||||
<textarea class="form-control" id="description" name="description"></textarea>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Cuisines</label>
|
||||
<div class="row">
|
||||
<?php foreach ($cuisines as $cuisine): ?>
|
||||
<div class="col-md-3">
|
||||
<div class="form-check">
|
||||
<input class="form-check-input" type="checkbox" name="cuisines[]" value="<?php echo $cuisine['id']; ?>" id="cuisine_<?php echo $cuisine['id']; ?>">
|
||||
<label class="form-check-label" for="cuisine_<?php echo $cuisine['id']; ?>">
|
||||
<?php echo htmlspecialchars($cuisine['name']); ?>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="image_url" class="form-label">Image URL</label>
|
||||
@ -50,4 +88,4 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<?php include 'footer.php'; ?>
|
||||
<?php include 'footer.php'; ?>
|
||||
79
admin/cuisines.php
Normal file
79
admin/cuisines.php
Normal file
@ -0,0 +1,79 @@
|
||||
<?php
|
||||
require_once '../db/config.php';
|
||||
require_once 'header.php';
|
||||
|
||||
$db = db();
|
||||
|
||||
// Handle form submissions for adding, editing, and deleting cuisines
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
if (isset($_POST['add_cuisine'])) {
|
||||
$name = trim($_POST['name']);
|
||||
if (!empty($name)) {
|
||||
$stmt = $db->prepare("INSERT INTO cuisines (name) VALUES (:name) ON CONFLICT (name) DO NOTHING");
|
||||
$stmt->bindValue(':name', $name, PDO::PARAM_STR);
|
||||
$stmt->execute();
|
||||
}
|
||||
} elseif (isset($_POST['delete_cuisine'])) {
|
||||
$id = $_POST['id'];
|
||||
$stmt = $db->prepare("DELETE FROM cuisines WHERE id = :id");
|
||||
$stmt->bindValue(':id', $id, PDO::PARAM_INT);
|
||||
$stmt->execute();
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch all cuisines
|
||||
$cuisines = $db->query("SELECT * FROM cuisines ORDER BY name ASC")->fetchAll();
|
||||
|
||||
?>
|
||||
|
||||
<div class="container mx-auto px-4 py-8">
|
||||
<h1 class="text-3xl font-bold mb-6">Manage Cuisines</h1>
|
||||
|
||||
<!-- Add Cuisine Form -->
|
||||
<div class="bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4">
|
||||
<h2 class="text-2xl mb-4">Add New Cuisine</h2>
|
||||
<form action="cuisines.php" method="POST">
|
||||
<div class="mb-4">
|
||||
<label class="block text-gray-700 text-sm font-bold mb-2" for="name">
|
||||
Cuisine Name
|
||||
</label>
|
||||
<input class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="name" name="name" type="text" placeholder="e.g., Italian, Mexican" required>
|
||||
</div>
|
||||
<div class="flex items-center justify-between">
|
||||
<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline" type="submit" name="add_cuisine">
|
||||
Add Cuisine
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<!-- Cuisines Table -->
|
||||
<div class="bg-white shadow-md rounded px-8 pt-6 pb-8">
|
||||
<h2 class="text-2xl mb-4">Existing Cuisines</h2>
|
||||
<table class="min-w-full table-auto">
|
||||
<thead class="bg-gray-200">
|
||||
<tr>
|
||||
<th class="px-4 py-2">ID</th>
|
||||
<th class="px-4 py-2">Name</th>
|
||||
<th class="px-4 py-2">Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php foreach ($cuisines as $cuisine): ?>
|
||||
<tr class="border-b">
|
||||
<td class="px-4 py-2 text-center"><?php echo htmlspecialchars($cuisine['id']); ?></td>
|
||||
<td class="px-4 py-2"><?php echo htmlspecialchars($cuisine['name']); ?></td>
|
||||
<td class="px-4 py-2 text-center">
|
||||
<form action="cuisines.php" method="POST" onsubmit="return confirm('Are you sure you want to delete this cuisine?');">
|
||||
<input type="hidden" name="id" value="<?php echo $cuisine['id']; ?>">
|
||||
<button type="submit" name="delete_cuisine" class="text-red-500 hover:text-red-700">Delete</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<?php require_once 'footer.php'; ?>
|
||||
@ -18,20 +18,42 @@ $pdo = db();
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$name = $_POST['name'] ?? '';
|
||||
$cuisine = $_POST['cuisine'] ?? '';
|
||||
$description = $_POST['description'] ?? '';
|
||||
$image_url = $_POST['image_url'] ?? '';
|
||||
$selected_cuisines = $_POST['cuisines'] ?? [];
|
||||
$id = $_POST['id'] ?? null;
|
||||
|
||||
if ($name && $cuisine && $id) {
|
||||
$stmt = $pdo->prepare("UPDATE restaurants SET name = ?, cuisine = ?, image_url = ? WHERE id = ?");
|
||||
$stmt->execute([$name, $cuisine, $image_url, $id]);
|
||||
header('Location: index.php');
|
||||
exit;
|
||||
if ($name && !empty($selected_cuisines) && $id) {
|
||||
try {
|
||||
$pdo->beginTransaction();
|
||||
|
||||
// Update restaurants table
|
||||
$stmt = $pdo->prepare("UPDATE restaurants SET name = ?, description = ?, image_url = ? WHERE id = ?");
|
||||
$stmt->execute([$name, $description, $image_url, $id]);
|
||||
|
||||
// Delete existing cuisine associations
|
||||
$delete_stmt = $pdo->prepare("DELETE FROM restaurant_cuisines WHERE restaurant_id = ?");
|
||||
$delete_stmt->execute([$id]);
|
||||
|
||||
// Insert new cuisine associations
|
||||
$cuisine_stmt = $pdo->prepare("INSERT INTO restaurant_cuisines (restaurant_id, cuisine_id) VALUES (?, ?)");
|
||||
foreach ($selected_cuisines as $cuisine_id) {
|
||||
$cuisine_stmt->execute([$id, $cuisine_id]);
|
||||
}
|
||||
|
||||
$pdo->commit();
|
||||
header('Location: index.php');
|
||||
exit;
|
||||
} catch (Exception $e) {
|
||||
$pdo->rollBack();
|
||||
$error = "Error updating restaurant: " . $e->getMessage();
|
||||
}
|
||||
} else {
|
||||
$error = "Name and cuisine are required.";
|
||||
$error = "Name and at least one cuisine are required.";
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch restaurant details
|
||||
$stmt = $pdo->prepare("SELECT * FROM restaurants WHERE id = ?");
|
||||
$stmt->execute([$id]);
|
||||
$restaurant = $stmt->fetch();
|
||||
@ -40,6 +62,16 @@ if (!$restaurant) {
|
||||
header('Location: index.php');
|
||||
exit;
|
||||
}
|
||||
|
||||
// Fetch all cuisines
|
||||
$cuisines_stmt = $pdo->query("SELECT * FROM cuisines ORDER BY name ASC");
|
||||
$cuisines = $cuisines_stmt->fetchAll();
|
||||
|
||||
// Fetch this restaurant's cuisines
|
||||
$restaurant_cuisines_stmt = $pdo->prepare("SELECT cuisine_id FROM restaurant_cuisines WHERE restaurant_id = ?");
|
||||
$restaurant_cuisines_stmt->execute([$id]);
|
||||
$restaurant_cuisine_ids = $restaurant_cuisines_stmt->fetchAll(PDO::FETCH_COLUMN);
|
||||
|
||||
?>
|
||||
|
||||
<div class="container mt-4">
|
||||
@ -56,16 +88,31 @@ if (!$restaurant) {
|
||||
<input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($restaurant['name']); ?>" required>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="cuisine" class="form-label">Cuisine</label>
|
||||
<input type="text" class="form-control" id="cuisine" name="cuisine" value="<?php echo htmlspecialchars($restaurant['cuisine']); ?>" required>
|
||||
<label for="description" class="form-label">Description</label>
|
||||
<textarea class="form-control" id="description" name="description"><?php echo htmlspecialchars($restaurant['description']); ?></textarea>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Cuisines</label>
|
||||
<div class="row">
|
||||
<?php foreach ($cuisines as $cuisine): ?>
|
||||
<div class="col-md-3">
|
||||
<div class="form-check">
|
||||
<input class="form-check-input" type="checkbox" name="cuisines[]" value="<?php echo $cuisine['id']; ?>" id="cuisine_<?php echo $cuisine['id']; ?>" <?php echo in_array($cuisine['id'], $restaurant_cuisine_ids) ? 'checked' : ''; ?>>
|
||||
<label class="form-check-label" for="cuisine_<?php echo $cuisine['id']; ?>">
|
||||
<?php echo htmlspecialchars($cuisine['name']); ?>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="image_url" class="form-label">Image URL</label>
|
||||
<input type="text" class_="form-control" id="image_url" name="image_url" value="<?php echo htmlspecialchars($restaurant['image_url']); ?>">
|
||||
<input type="text" class="form-control" id="image_url" name="image_url" value="<?php echo htmlspecialchars($restaurant['image_url']); ?>">
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">Update Restaurant</button>
|
||||
<a href="index.php" class="btn btn-secondary">Cancel</a>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<?php include 'footer.php'; ?>
|
||||
<?php include 'footer.php'; ?>
|
||||
@ -31,6 +31,9 @@ if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== tru
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="coupons.php">Coupons</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="cuisines.php">Cuisines</a>
|
||||
</li>
|
||||
</ul>
|
||||
<ul class="navbar-nav ms-auto">
|
||||
<li class="nav-item">
|
||||
|
||||
230
index.php
230
index.php
@ -4,91 +4,169 @@
|
||||
<section class="hero">
|
||||
<div class="hero-content">
|
||||
<h1>Order from Majuro's best</h1>
|
||||
<form action="search.php" method="get" class="search-form">
|
||||
<input type="text" name="query" class="search-bar" placeholder="Search for restaurants, cuisines..." required>
|
||||
<form action="index.php" method="get" class="search-form">
|
||||
<input type="text" name="search" class="search-bar" placeholder="Search for restaurants..." value="<?= isset($_GET['search']) ? htmlspecialchars($_GET['search']) : '' ?>">
|
||||
<button type="submit" class="search-button">Search</button>
|
||||
</form>
|
||||
<div class="filter-bar">
|
||||
<div class="filter-dropdown">
|
||||
<button class="filter-button">
|
||||
<span class="filter-button-text"><?= isset($_GET['cuisine']) && $_GET['cuisine'] != '' ? htmlspecialchars($_GET['cuisine']) : 'All Cuisines' ?></span>
|
||||
<span class="filter-button-icon">▼</span>
|
||||
</button>
|
||||
<div class="filter-options">
|
||||
<a href="index.php<?= isset($_GET['search']) ? '?search=' . urlencode($_GET['search']) : '' ?>" class="filter-option <?= !isset($_GET['cuisine']) || $_GET['cuisine'] == '' ? 'active' : '' ?>">All Cuisines</a>
|
||||
<?php
|
||||
$cuisine_stmt = $pdo->query("SELECT DISTINCT cuisine FROM restaurants ORDER BY cuisine");
|
||||
$cuisines = $cuisine_stmt->fetchAll(PDO::FETCH_COLUMN);
|
||||
$search_param = isset($_GET['search']) ? '&search=' . urlencode($_GET['search']) : '';
|
||||
foreach ($cuisines as $cuisine):
|
||||
?>
|
||||
<a href="index.php?cuisine=<?= htmlspecialchars($cuisine) ?><?= $search_param ?>" class="filter-option <?= (isset($_GET['cuisine']) && $_GET['cuisine'] == $cuisine) ? 'active' : '' ?>"><?= htmlspecialchars($cuisine) ?></a>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<div class="container">
|
||||
<h2 class="page-title">All Restaurants</h2>
|
||||
<div class="row">
|
||||
<!-- Filter Sidebar -->
|
||||
<div class="col-md-3">
|
||||
<h4>Filter by Cuisine</h4>
|
||||
<form action="index.php" method="get" id="filter-form">
|
||||
<!-- Hidden search field to persist search query -->
|
||||
<?php if (isset($_GET['search'])): ?>
|
||||
<input type="hidden" name="search" value="<?= htmlspecialchars($_GET['search']) ?>">
|
||||
<?php endif; ?>
|
||||
|
||||
<section class="restaurant-list">
|
||||
<div class="restaurant-grid" id="restaurant-grid">
|
||||
<?php
|
||||
// Base query
|
||||
$sql = "SELECT r.id, r.name, r.cuisine, r.image_url, AVG(rt.rating) as average_rating, COUNT(rt.id) as rating_count FROM restaurants r LEFT JOIN ratings rt ON r.id = rt.restaurant_id";
|
||||
$params = [];
|
||||
$where_clauses = [];
|
||||
<?php
|
||||
$cuisine_stmt = $pdo->query("SELECT * FROM cuisines ORDER BY name");
|
||||
$all_cuisines = $cuisine_stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
$selected_cuisines = isset($_GET['cuisines']) && is_array($_GET['cuisines']) ? $_GET['cuisines'] : [];
|
||||
|
||||
// Append search condition
|
||||
if (!empty($_GET['search'])) {
|
||||
$where_clauses[] = "r.name LIKE ?";
|
||||
$params[] = '%' . $_GET['search'] . '%';
|
||||
}
|
||||
|
||||
// Append cuisine filter
|
||||
if (!empty($_GET['cuisine'])) {
|
||||
$where_clauses[] = "r.cuisine = ?";
|
||||
$params[] = $_GET['cuisine'];
|
||||
}
|
||||
|
||||
if (!empty($where_clauses)) {
|
||||
$sql .= " WHERE " . implode(' AND ', $where_clauses);
|
||||
}
|
||||
|
||||
$sql .= " GROUP BY r.id, r.name, r.cuisine, r.image_url ORDER BY r.name";
|
||||
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute($params);
|
||||
$restaurants = $stmt->fetchAll();
|
||||
|
||||
if (empty($restaurants)) {
|
||||
echo '<p>No restaurants found matching your criteria.</p>';
|
||||
} else {
|
||||
foreach ($restaurants as $restaurant) {
|
||||
echo '<a href="menu.php?id=' . htmlspecialchars($restaurant['id']) . '" class="restaurant-card">';
|
||||
echo '<img src="' . htmlspecialchars($restaurant['image_url']) . '" alt="' . htmlspecialchars($restaurant['name']) . '">';
|
||||
echo '<div class="restaurant-card-content">';
|
||||
echo '<h3>' . htmlspecialchars($restaurant['name']) . '</h3>';
|
||||
echo '<p>' . htmlspecialchars($restaurant['cuisine']) . '</p>';
|
||||
if ($restaurant['rating_count'] > 0) {
|
||||
echo '<div class="rating-display">';
|
||||
echo '<span class="star">★</span>';
|
||||
echo '<span>' . htmlspecialchars(number_format($restaurant['average_rating'], 1)) . '</span>';
|
||||
echo '<span class="rating-count">(' . htmlspecialchars($restaurant['rating_count']) . ' ratings)</span>';
|
||||
echo '</div>';
|
||||
} else {
|
||||
echo '<div class="rating-display"><span class="rating-count">No ratings yet</span></div>';
|
||||
}
|
||||
echo '</div>';
|
||||
echo '</a>';
|
||||
}
|
||||
}
|
||||
?>
|
||||
foreach ($all_cuisines as $cuisine): ?>
|
||||
<div class="form-check">
|
||||
<input class="form-check-input" type="checkbox" name="cuisines[]" value="<?= $cuisine['id'] ?>" id="cuisine-<?= $cuisine['id'] ?>" <?= in_array($cuisine['id'], $selected_cuisines) ? 'checked' : '' ?>>
|
||||
<label class="form-check-label" for="cuisine-<?= $cuisine['id'] ?>">
|
||||
<?= htmlspecialchars($cuisine['name']) ?>
|
||||
</label>
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
<button type="submit" class="btn btn-primary mt-3">Apply Filter</button>
|
||||
<a href="index.php" class="btn btn-secondary mt-3">Clear Filter</a>
|
||||
</form>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<!-- Restaurant Listing -->
|
||||
<div class="col-md-9">
|
||||
<h2 class="page-title">All Restaurants</h2>
|
||||
<section class="restaurant-list">
|
||||
<div class="restaurant-grid" id="restaurant-grid">
|
||||
<?php
|
||||
// Base query
|
||||
$sql = "SELECT DISTINCT r.id, r.name, r.image_url, AVG(rt.rating) as average_rating, COUNT(rt.id) as rating_count
|
||||
FROM restaurants r
|
||||
LEFT JOIN ratings rt ON r.id = rt.restaurant_id";
|
||||
$params = [];
|
||||
|
||||
// Join with restaurant_cuisines if filtering by cuisine
|
||||
if (!empty($selected_cuisines)) {
|
||||
$sql .= " JOIN restaurant_cuisines rc ON r.id = rc.restaurant_id";
|
||||
}
|
||||
|
||||
$where_clauses = [];
|
||||
|
||||
// Append search condition
|
||||
if (!empty($_GET['search'])) {
|
||||
$where_clauses[] = "r.name LIKE ?";
|
||||
$params[] = '%' . $_GET['search'] . '%';
|
||||
}
|
||||
|
||||
// Append cuisine filter
|
||||
if (!empty($selected_cuisines)) {
|
||||
$placeholders = implode(',', array_fill(0, count($selected_cuisines), '?'));
|
||||
$where_clauses[] = "rc.cuisine_id IN ($placeholders)";
|
||||
$params = array_merge($params, $selected_cuisines);
|
||||
}
|
||||
|
||||
if (!empty($where_clauses)) {
|
||||
$sql .= " WHERE " . implode(' AND ', $where_clauses);
|
||||
}
|
||||
|
||||
$sql .= " GROUP BY r.id, r.name, r.image_url ORDER BY r.name";
|
||||
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute($params);
|
||||
$restaurants = $stmt->fetchAll();
|
||||
|
||||
if (empty($restaurants)) {
|
||||
echo '<p>No restaurants found matching your criteria.</p>';
|
||||
} else {
|
||||
foreach ($restaurants as $restaurant) {
|
||||
// Get cuisines for this restaurant
|
||||
$cuisine_sql = "SELECT c.name FROM cuisines c JOIN restaurant_cuisines rc ON c.id = rc.cuisine_id WHERE rc.restaurant_id = ?";
|
||||
$cuisine_stmt = $pdo->prepare($cuisine_sql);
|
||||
$cuisine_stmt->execute([$restaurant['id']]);
|
||||
$restaurant_cuisines_list = $cuisine_stmt->fetchAll(PDO::FETCH_COLUMN);
|
||||
|
||||
echo '<a href="menu.php?id=' . htmlspecialchars($restaurant['id']) . '" class="restaurant-card">';
|
||||
echo '<img src="' . htmlspecialchars($restaurant['image_url'] ? $restaurant['image_url'] : 'assets/images/hero.jpg') . '" alt="' . htmlspecialchars($restaurant['name']) . '">';
|
||||
echo '<div class="restaurant-card-content">';
|
||||
echo '<h3>' . htmlspecialchars($restaurant['name']) . '</h3>';
|
||||
echo '<p>' . htmlspecialchars(implode(', ', $restaurant_cuisines_list)) . '</p>';
|
||||
if ($restaurant['rating_count'] > 0) {
|
||||
echo '<div class="rating-display">';
|
||||
echo '<span class="star">★</span>';
|
||||
echo '<span>' . htmlspecialchars(number_format($restaurant['average_rating'], 1)) . '</span>';
|
||||
echo '<span class="rating-count">(' . htmlspecialchars($restaurant['rating_count']) . ' ratings)</span>';
|
||||
echo '</div>';
|
||||
} else {
|
||||
echo '<div class="rating-display"><span class="rating-count">No ratings yet</span></div>';
|
||||
}
|
||||
echo '</div>';
|
||||
echo '</a>';
|
||||
}
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<?php include 'footer.php'; ?>
|
||||
<style>
|
||||
.container {
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
padding: 20px;
|
||||
}
|
||||
.row {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
margin: 0 -15px;
|
||||
}
|
||||
.col-md-3 {
|
||||
width: 25%;
|
||||
padding: 0 15px;
|
||||
}
|
||||
.col-md-9 {
|
||||
width: 75%;
|
||||
padding: 0 15px;
|
||||
}
|
||||
.form-check {
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
.btn {
|
||||
padding: 8px 15px;
|
||||
border-radius: 5px;
|
||||
text-decoration: none;
|
||||
cursor: pointer;
|
||||
display: inline-block;
|
||||
text-align: center;
|
||||
}
|
||||
.btn-primary {
|
||||
background-color: #007bff;
|
||||
color: white;
|
||||
border: 1px solid #007bff;
|
||||
}
|
||||
.btn-secondary {
|
||||
background-color: #6c757d;
|
||||
color: white;
|
||||
border: 1px solid #6c757d;
|
||||
}
|
||||
.mt-3 {
|
||||
margin-top: 1rem;
|
||||
}
|
||||
.search-form {
|
||||
display: flex;
|
||||
}
|
||||
.search-bar {
|
||||
flex-grow: 1;
|
||||
}
|
||||
</style>
|
||||
|
||||
<?php include 'footer.php'; ?>
|
||||
|
||||
1
migrations/20251015_add_order_id_to_ratings.sql
Normal file
1
migrations/20251015_add_order_id_to_ratings.sql
Normal file
@ -0,0 +1 @@
|
||||
ALTER TABLE ratings ADD COLUMN order_id INT;
|
||||
20
migrations/20251015_create_cuisines_tables.sql
Normal file
20
migrations/20251015_create_cuisines_tables.sql
Normal file
@ -0,0 +1,20 @@
|
||||
-- Create cuisines table
|
||||
CREATE TABLE IF NOT EXISTS cuisines (
|
||||
id SERIAL PRIMARY KEY,
|
||||
name VARCHAR(255) NOT NULL UNIQUE
|
||||
);
|
||||
|
||||
-- Create restaurant_cuisines linking table for many-to-many relationship
|
||||
CREATE TABLE IF NOT EXISTS restaurant_cuisines (
|
||||
restaurant_id INT NOT NULL,
|
||||
cuisine_id INT NOT NULL,
|
||||
PRIMARY KEY (restaurant_id, cuisine_id),
|
||||
CONSTRAINT fk_restaurant
|
||||
FOREIGN KEY(restaurant_id)
|
||||
REFERENCES restaurants(id)
|
||||
ON DELETE CASCADE,
|
||||
CONSTRAINT fk_cuisine
|
||||
FOREIGN KEY(cuisine_id)
|
||||
REFERENCES cuisines(id)
|
||||
ON DELETE CASCADE
|
||||
);
|
||||
48
rate.php
48
rate.php
@ -1,48 +0,0 @@
|
||||
<?php
|
||||
session_start();
|
||||
require_once 'db/config.php';
|
||||
|
||||
if (!isset($_SESSION['user_id'])) {
|
||||
header("Location: login.php");
|
||||
exit();
|
||||
}
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$user_id = $_SESSION['user_id'];
|
||||
$order_id = $_POST['order_id'];
|
||||
$restaurant_id = $_POST['restaurant_id'];
|
||||
$rating = $_POST['rating'];
|
||||
$comment = $_POST['comment'];
|
||||
|
||||
// Validation
|
||||
if (empty($order_id) || empty($restaurant_id) || empty($rating) || $rating < 1 || $rating > 5) {
|
||||
// Handle error - redirect back to profile with an error message
|
||||
$_SESSION['rating_error'] = "Invalid data provided.";
|
||||
header("Location: profile.php");
|
||||
exit();
|
||||
}
|
||||
|
||||
// Check if the user has already rated this order
|
||||
$stmt = $db()->prepare("SELECT id FROM ratings WHERE user_id = ? AND order_id = ?");
|
||||
$stmt->execute([$user_id, $order_id]);
|
||||
if ($stmt->fetch()) {
|
||||
$_SESSION['rating_error'] = "You have already rated this order.";
|
||||
header("Location: profile.php");
|
||||
exit();
|
||||
}
|
||||
|
||||
// Insert the rating
|
||||
$stmt = $db()->prepare("INSERT INTO ratings (user_id, order_id, restaurant_id, rating, comment) VALUES (?, ?, ?, ?, ?)");
|
||||
if ($stmt->execute([$user_id, $order_id, $restaurant_id, $rating, $comment])) {
|
||||
$_SESSION['rating_success'] = "Thank you for your feedback!";
|
||||
} else {
|
||||
$_SESSION['rating_error'] = "Something went wrong. Please try again.";
|
||||
}
|
||||
|
||||
header("Location: profile.php");
|
||||
exit();
|
||||
} else {
|
||||
header("Location: profile.php");
|
||||
exit();
|
||||
}
|
||||
?>
|
||||
48
search.php
48
search.php
@ -1,48 +0,0 @@
|
||||
<?php
|
||||
require_once 'header.php';
|
||||
require_once 'db/config.php';
|
||||
|
||||
$query = $_GET['query'] ?? '';
|
||||
|
||||
$db = db();
|
||||
|
||||
if ($query) {
|
||||
$stmt = $db->prepare(
|
||||
'SELECT r.*, COALESCE(AVG(ra.rating), 0) as rating, COUNT(ra.id) as rating_count '
|
||||
. 'FROM restaurants r LEFT JOIN ratings ra ON r.id = ra.restaurant_id '
|
||||
. 'WHERE r.name LIKE ? OR r.cuisine LIKE ? GROUP BY r.id'
|
||||
);
|
||||
$stmt->execute(['%' . $query . '%', '%' . $query . '%']);
|
||||
$restaurants = $stmt->fetchAll();
|
||||
} else {
|
||||
$restaurants = [];
|
||||
}
|
||||
?>
|
||||
|
||||
<main class="container">
|
||||
<h1 class="page-title">Search Results for "<?php echo htmlspecialchars($query); ?>"</h1>
|
||||
|
||||
<div class="restaurant-list">
|
||||
<?php if (empty($restaurants)): ?>
|
||||
<p>No restaurants found.</p>
|
||||
<?php else: ?>
|
||||
<?php foreach ($restaurants as $restaurant): ?>
|
||||
<div class="restaurant-card">
|
||||
<a href="menu.php?restaurant_id=<?php echo $restaurant['id']; ?>">
|
||||
<img src="<?php echo htmlspecialchars($restaurant['image_url']); ?>" alt="<?php echo htmlspecialchars($restaurant['name']); ?>">
|
||||
<div class="restaurant-info">
|
||||
<h3><?php echo htmlspecialchars($restaurant['name']); ?></h3>
|
||||
<p><?php echo htmlspecialchars($restaurant['cuisine']); ?></p>
|
||||
<div class="rating">
|
||||
<span>★ <?php echo number_format($restaurant['rating'], 1); ?></span>
|
||||
<span>(<?php echo $restaurant['rating_count']; ?>)</span>
|
||||
</div>
|
||||
</div>
|
||||
</a>
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<?php require_once 'footer.php'; ?>
|
||||
Loading…
x
Reference in New Issue
Block a user