This commit is contained in:
Flatlogic Bot 2025-12-15 09:46:36 +00:00
parent 08c2df845a
commit fdcc78312c
3 changed files with 75 additions and 10 deletions

View File

@ -59,6 +59,7 @@
<th>Address</th>
<th>Phone</th>
<th>Email</th>
<th>Cuisine</th>
<th>Actions</th>
</tr>
</thead>
@ -97,6 +98,10 @@
<label for="email" class="form-label">Contact Email</label>
<input type="email" class="form-control" id="email" name="email" 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>
</div>
<button type="submit" class="btn btn-primary">Save Restaurant</button>
</form>
</div>
@ -185,6 +190,7 @@
<td data-field="address">${r.address}</td>
<td data-field="phone">${r.phone}</td>
<td data-field="email">${r.email}</td>
<td data-field="cuisine">${r.cuisine}</td>
<td>
<a href="restaurant_menu.php?restaurant_id=${r.id}" class="btn btn-sm btn-info menu-btn" title="Manage Menu"><i class="bi bi-card-list"></i></a>
<button class="btn btn-sm btn-secondary edit-btn" title="Edit Restaurant"><i class="bi bi-pencil"></i></button>
@ -210,6 +216,7 @@
document.getElementById('address').value = row.querySelector('[data-field="address"]').textContent;
document.getElementById('phone').value = row.querySelector('[data-field="phone"]').textContent;
document.getElementById('email').value = row.querySelector('[data-field="email"]').textContent;
document.getElementById('cuisine').value = row.querySelector('[data-field="cuisine"]').textContent;
restaurantModalLabel.textContent = 'Edit Restaurant';
restaurantModal.show();

View File

@ -27,7 +27,7 @@ switch ($method) {
function handle_get() {
try {
$pdo = db();
$stmt = $pdo->query("SELECT id, name, address, phone, email FROM restaurants ORDER BY created_at DESC");
$stmt = $pdo->query("SELECT id, name, cuisine, address, phone, email FROM restaurants ORDER BY created_at DESC");
$restaurants = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode(['success' => true, 'data' => $restaurants]);
} catch (PDOException $e) {
@ -39,7 +39,7 @@ function handle_get() {
function handle_post() {
$data = json_decode(file_get_contents('php://input'), true);
if (empty($data['name']) || empty($data['address']) || empty($data['phone']) || empty($data['email'])) {
if (empty($data['name']) || empty($data['address']) || empty($data['phone']) || empty($data['email']) || empty($data['cuisine'])) {
header('HTTP/1.1 400 Bad Request');
echo json_encode(['success' => false, 'error' => 'All fields are required.']);
return;
@ -47,10 +47,11 @@ function handle_post() {
try {
$pdo = db();
$sql = "INSERT INTO restaurants (name, address, phone, email) VALUES (:name, :address, :phone, :email)";
$sql = "INSERT INTO restaurants (name, cuisine, address, phone, email) VALUES (:name, :cuisine, :address, :phone, :email)";
$stmt = $pdo->prepare($sql);
$stmt->execute([
':name' => $data['name'],
':cuisine' => $data['cuisine'],
':address' => $data['address'],
':phone' => $data['phone'],
':email' => $data['email'],
@ -59,7 +60,7 @@ function handle_post() {
$lastInsertId = $pdo->lastInsertId();
// Fetch the created restaurant to return it
$stmt = $pdo->prepare("SELECT id, name, address, phone, email FROM restaurants WHERE id = :id");
$stmt = $pdo->prepare("SELECT id, name, cuisine, address, phone, email FROM restaurants WHERE id = :id");
$stmt->execute(['id' => $lastInsertId]);
$newRestaurant = $stmt->fetch(PDO::FETCH_ASSOC);
@ -73,7 +74,7 @@ function handle_post() {
function handle_put() {
$data = json_decode(file_get_contents('php://input'), true);
if (empty($data['id']) || empty($data['name']) || empty($data['address']) || empty($data['phone']) || empty($data['email'])) {
if (empty($data['id']) || empty($data['name']) || empty($data['address']) || empty($data['phone']) || empty($data['email']) || empty($data['cuisine'])) {
header('HTTP/1.1 400 Bad Request');
echo json_encode(['success' => false, 'error' => 'All fields including ID are required.']);
return;
@ -81,11 +82,12 @@ function handle_put() {
try {
$pdo = db();
$sql = "UPDATE restaurants SET name = :name, address = :address, phone = :phone, email = :email WHERE id = :id";
$sql = "UPDATE restaurants SET name = :name, cuisine = :cuisine, address = :address, phone = :phone, email = :email WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute([
':id' => $data['id'],
':name' => $data['name'],
':cuisine' => $data['cuisine'],
':address' => $data['address'],
':phone' => $data['phone'],
':email' => $data['email'],

View File

@ -2,12 +2,15 @@
require_once 'db/config.php';
$restaurants = [];
$cuisines = [];
try {
$pdo = db();
$stmt = $pdo->query("SELECT id, name, address, phone, email FROM restaurants ORDER BY name ASC");
$stmt = $pdo->query("SELECT id, name, cuisine, address, phone, email FROM restaurants ORDER BY name ASC");
$restaurants = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt = $pdo->query("SELECT DISTINCT cuisine FROM restaurants WHERE cuisine IS NOT NULL AND cuisine != '' ORDER BY cuisine ASC");
$cuisines = $stmt->fetchAll(PDO::FETCH_COLUMN);
} catch (PDOException $e) {
// For a real app, you would log this error and show a user-friendly message.
error_log("Database error: " . $e->getMessage());
}
@ -58,17 +61,32 @@ try {
</header>
<main class="container my-5">
<div class="row">
<div class="row mb-4">
<div class="col-md-8">
<input type="text" id="searchInput" class="form-control" placeholder="Search by restaurant name...">
</div>
<div class="col-md-4">
<select id="cuisineFilter" class="form-select">
<option value="">All Cuisines</option>
<?php foreach ($cuisines as $cuisine): ?>
<option value="<?= htmlspecialchars($cuisine) ?>"><?= htmlspecialchars($cuisine) ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
<div class="row" id="restaurantList">
<?php if (empty($restaurants)): ?>
<div class="col">
<p class="text-center text-muted">No restaurants are available at the moment. Please check back later.</p>
</div>
<?php else: ?>
<?php foreach ($restaurants as $restaurant): ?>
<div class="col-md-4 mb-4">
<div class="col-md-4 mb-4 restaurant-item" data-name="<?= htmlspecialchars(strtolower($restaurant['name'])) ?>" data-cuisine="<?= htmlspecialchars(strtolower($restaurant['cuisine'])) ?>">
<div class="card h-100 restaurant-card">
<div class="card-body d-flex flex-column">
<h5 class="card-title"><?= htmlspecialchars($restaurant['name']) ?></h5>
<p class="card-text"><span class="badge bg-secondary"><?= htmlspecialchars($restaurant['cuisine']) ?></span></p>
<p class="card-text text-muted flex-grow-1"><?= htmlspecialchars($restaurant['address']) ?></p>
<a href="menu.php?restaurant_id=<?= $restaurant['id'] ?>" class="btn btn-primary mt-auto">View Menu</a>
</div>
@ -77,11 +95,49 @@ try {
<?php endforeach; ?>
<?php endif; ?>
</div>
<div id="noResults" class="text-center text-muted" style="display: none;">
<p>No restaurants match your search.</p>
</div>
</main>
<footer class="text-center text-muted py-4">
<p>&copy; <?= date('Y') ?> Food Marketplace</p>
</footer>
<script>
document.addEventListener('DOMContentLoaded', function () {
const searchInput = document.getElementById('searchInput');
const cuisineFilter = document.getElementById('cuisineFilter');
const restaurantList = document.getElementById('restaurantList');
const restaurantItems = restaurantList.querySelectorAll('.restaurant-item');
const noResults = document.getElementById('noResults');
function filterRestaurants() {
const searchTerm = searchInput.value.toLowerCase();
const cuisineTerm = cuisineFilter.value.toLowerCase();
let resultsFound = false;
restaurantItems.forEach(item => {
const name = item.dataset.name;
const cuisine = item.dataset.cuisine;
const nameMatch = name.includes(searchTerm);
const cuisineMatch = cuisineTerm === '' || cuisine.includes(cuisineTerm);
if (nameMatch && cuisineMatch) {
item.style.display = '';
resultsFound = true;
} else {
item.style.display = 'none';
}
});
noResults.style.display = resultsFound ? 'none' : '';
}
searchInput.addEventListener('input', filterRestaurants);
cuisineFilter.addEventListener('change', filterRestaurants);
});
</script>
</body>
</html>