88 lines
4.6 KiB
PHP
88 lines
4.6 KiB
PHP
<?php include 'header.php'; ?>
|
|
|
|
<main>
|
|
<section class="hero">
|
|
<div class="hero-content">
|
|
<h1>Order from Majuro's best</h1>
|
|
<form action="index.php" method="get" class="search-form">
|
|
<input type="text" name="search" class="search-bar" placeholder="Search restaurants..." value="<?= isset($_GET['search']) ? htmlspecialchars($_GET['search']) : '' ?>">
|
|
<button type="submit" class="search-button">Search</button>
|
|
</form>
|
|
<div class="cuisine-filters">
|
|
<a href="index.php<?= isset($_GET['search']) ? '?search=' . urlencode($_GET['search']) : '' ?>" class="cuisine-filter-link <?= !isset($_GET['cuisine']) || $_GET['cuisine'] == '' ? 'active' : '' ?>">All Cuisines</a>
|
|
<?php
|
|
require_once 'db/config.php';
|
|
$pdo = db();
|
|
$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="cuisine-filter-link <?= (isset($_GET['cuisine']) && $_GET['cuisine'] == $cuisine) ? 'active' : '' ?>"><?= htmlspecialchars($cuisine) ?></a>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
|
|
<div class="container">
|
|
<h2 class="page-title">All Restaurants</h2>
|
|
|
|
<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 = [];
|
|
|
|
// 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>';
|
|
}
|
|
}
|
|
?>
|
|
</div>
|
|
</section>
|
|
</div>
|
|
</main>
|
|
|
|
<?php include 'footer.php'; ?>
|