34968-vm/index.php
Flatlogic Bot 42cfa7481c V19
2025-10-16 08:36:38 +00:00

133 lines
5.1 KiB
PHP

<?php
// Handle AJAX request for nearby restaurants
if (isset($_GET['action']) && $_GET['action'] == 'get_restaurants' && isset($_GET['lat']) && isset($_GET['lng'])) {
require_once 'db/config.php';
header('Content-Type: application/json');
$lat = (float)$_GET['lat'];
$lng = (float)$_GET['lng'];
$radius = 10; // Search radius in kilometers
$db = db();
// Haversine formula to calculate distance
$stmt = $db->prepare("
SELECT r.id, r.name, r.image_url, AVG(rt.rating) as average_rating,
(6371 * acos(cos(radians(?)) * cos(radians(latitude)) * cos(radians(longitude) - radians(?)) + sin(radians(?)) * sin(radians(latitude)))) AS distance
FROM restaurants r
LEFT JOIN ratings rt ON r.id = rt.restaurant_id
WHERE latitude IS NOT NULL AND longitude IS NOT NULL
GROUP BY r.id
HAVING distance < ?
ORDER BY distance
LIMIT 12
");
$stmt->execute([$lat, $lng, $lat, $radius]);
$restaurants = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Get cuisines for each 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 = $db->prepare($cuisine_sql);
foreach ($restaurants as &$restaurant) {
$cuisine_stmt->execute([$restaurant['id']]);
$restaurant['cuisines'] = $cuisine_stmt->fetchAll(PDO::FETCH_COLUMN);
}
echo json_encode($restaurants);
exit;
}
?>
<?php include 'header.php'; ?>
<?php include 'hero.php'; ?>
<div class="container page-content">
<section class="promo-section">
<div class="promo-card">
<h3>$0 Delivery Fee</h3>
<p>On your first order</p>
</div>
<div class="promo-card">
<h3>Earn Rewards</h3>
<p>With every meal</p>
</div>
<div class="promo-card">
<h3>Support Local</h3>
<p>Majuro Restaurants</p>
</div>
</section>
<?php
// Fetch Top-Rated Restaurants
require_once 'db/config.php';
$db = db();
$top_restaurants_stmt = $db->query("
SELECT r.id, r.name, r.image_url, GROUP_CONCAT(c.name SEPARATOR ', ') as cuisines, AVG(rt.rating) as average_rating
FROM restaurants r
LEFT JOIN restaurant_cuisines rc ON r.id = rc.restaurant_id
LEFT JOIN cuisines c ON rc.cuisine_id = c.id
LEFT JOIN ratings rt ON r.id = rt.restaurant_id
GROUP BY r.id
ORDER BY average_rating DESC
LIMIT 4
");
$top_restaurants = $top_restaurants_stmt->fetchAll(PDO::FETCH_ASSOC);
// Fetch Featured Cuisines
$featured_cuisines_stmt = $db->query("
SELECT c.id, c.name, c.image_url
FROM cuisines c
JOIN (
SELECT cuisine_id, COUNT(*) as restaurant_count
FROM restaurant_cuisines
GROUP BY cuisine_id
ORDER BY restaurant_count DESC
LIMIT 4
) as popular_cuisines ON c.id = popular_cuisines.cuisine_id
");
$featured_cuisines = $featured_cuisines_stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<section class="top-rated-restaurants">
<h2 class="section-title">Top-rated restaurants</h2>
<div class="restaurant-grid">
<?php foreach ($top_restaurants as $restaurant): ?>
<a href="menu.php?restaurant_id=<?php echo $restaurant['id']; ?>" class="restaurant-card">
<div class="card-image" style="background-image: url('<?php echo htmlspecialchars($restaurant['image_url'] ?: 'assets/images/hero.jpg'); ?>')"></div>
<div class="card-content">
<h3><?php echo htmlspecialchars($restaurant['name']); ?></h3>
<p class="cuisine-tags"><?php echo htmlspecialchars($restaurant['cuisines']); ?></p>
<div class="restaurant-info">
<div class="rating-display">
<?php if ($restaurant['average_rating']): ?>
<span class="star">★</span> <?php echo number_format($restaurant['average_rating'], 1); ?>
<?php else: ?>
New
<?php endif; ?>
</div>
</div>
</div>
</a>
<?php endforeach; ?>
</div>
</section>
<section class="featured-cuisines">
<h2 class="section-title">Featured Cuisines</h2>
<div class="cuisine-grid">
<?php foreach ($featured_cuisines as $cuisine): ?>
<a href="index.php?cuisine=<?php echo $cuisine['id']; ?>" class="cuisine-card">
<div class="card-image" style="background-image: url('<?php echo htmlspecialchars($cuisine['image_url'] ?: 'https://via.placeholder.com/300x200'); ?>')"></div>
<div class="cuisine-card-content">
<h3><?php echo htmlspecialchars($cuisine['name']); ?></h3>
</div>
</a>
<?php endforeach; ?>
</div>
</section>
</div>
<?php include 'footer.php'; ?>