37650-vm/car_list.php
Flatlogic Bot 73e14b3353 sad
2026-01-21 17:27:41 +00:00

146 lines
7.8 KiB
PHP

<?php
require_once __DIR__ . '/db/config.php';
$pdo = db();
// Filtering Logic
$whereClauses = ["status IN ('approved', 'sold')"];
$params = [];
if (!empty($_GET['make'])) {
$whereClauses[] = "make = ?";
$params[] = $_GET['make'];
}
if (!empty($_GET['province'])) {
$whereClauses[] = "province = ?";
$params[] = $_GET['province'];
}
if (!empty($_GET['min_price'])) {
$whereClauses[] = "price >= ?";
$params[] = $_GET['min_price'];
}
if (!empty($_GET['max_price'])) {
$whereClauses[] = "price <= ?";
$params[] = $_GET['max_price'];
}
if (!empty($_GET['search'])) {
$whereClauses[] = "(model LIKE ? OR description LIKE ?)";
$searchTerm = "%{$_GET['search']}%";
$params[] = $searchTerm;
$params[] = $searchTerm;
}
$sql = "SELECT * FROM cars";
if (!empty($whereClauses)) {
$sql .= " WHERE " . implode(' AND ', $whereClauses);
}
$sql .= " ORDER BY CASE WHEN status = 'approved' THEN 1 ELSE 2 END, created_at DESC";
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$cars = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Fetch distinct makes and provinces for filter dropdowns
$makes = $pdo->query("SELECT DISTINCT make FROM cars WHERE status IN ('approved', 'sold') ORDER BY make ASC")->fetchAll(PDO::FETCH_COLUMN);
$provinces = $pdo->query("SELECT DISTINCT province FROM cars WHERE status IN ('approved', 'sold') AND province IS NOT NULL ORDER BY province ASC")->fetchAll(PDO::FETCH_COLUMN);
$pageTitle = 'All Car Listings';
include 'partials/header.php';
?>
<header class="bg-light py-5">
<div class="container text-center">
<h1 class="display-4">Our Vehicle Collection</h1>
<p class="lead text-muted">Browse the complete inventory of our certified pre-owned vehicles from all over Afghanistan.</p>
</div>
</header>
<main class="container section-padding py-5">
<div class="row g-5">
<aside class="col-lg-3">
<div class="card p-4 sticky-top border-0 shadow-sm" style="top: 2rem;">
<h4 class="mb-4">Filter Results</h4>
<form method="GET">
<div class="mb-3">
<label for="search" class="form-label">Keyword</label>
<input type="text" name="search" id="search" class="form-control" placeholder="e.g., Corolla, V8" value="<?= htmlspecialchars($_GET['search'] ?? '') ?>">
</div>
<div class="mb-3">
<label for="make" class="form-label">Make</label>
<select name="make" id="make" class="form-select">
<option value="">All Makes</option>
<?php foreach ($makes as $make): ?>
<option value="<?= htmlspecialchars($make) ?>" <?= (($_GET['make'] ?? '') === $make) ? 'selected' : '' ?>><?= htmlspecialchars($make) ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label for="province" class="form-label">Province</label>
<select name="province" id="province" class="form-select">
<option value="">All Provinces</option>
<?php foreach ($provinces as $province): ?>
<option value="<?= htmlspecialchars($province) ?>" <?= (($_GET['province'] ?? '') === $province) ? 'selected' : '' ?>><?= htmlspecialchars($province) ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label class="form-label">Price Range ($)</label>
<div class="d-flex gap-2">
<input type="number" name="min_price" class="form-control" placeholder="Min" value="<?= htmlspecialchars($_GET['min_price'] ?? '') ?>">
<input type="number" name="max_price" class="form-control" placeholder="Max" value="<?= htmlspecialchars($_GET['max_price'] ?? '') ?>">
</div>
</div>
<div class="d-grid gap-2">
<button type="submit" class="btn btn-primary">Apply Filters</button>
<a href="car_list.php" class="btn btn-secondary">Reset Filters</a>
</div>
</form>
</div>
</aside>
<div class="col-lg-9">
<div class="row g-4">
<?php if (!empty($cars)): ?>
<?php foreach ($cars as $car): ?>
<div class="col-md-6 col-xl-4 d-flex align-items-stretch">
<div class="card w-100 border-0 shadow-sm">
<div class="position-relative">
<img src="<?= htmlspecialchars($car['image_url'] ?: 'https://via.placeholder.com/400x300?text=No+Image') ?>" class="card-img-top" alt="<?= htmlspecialchars($car['make'] . ' ' . $car['model']) ?>" style="height: 200px; object-fit: cover;">
<?php if ($car['status'] === 'sold'): ?>
<div class="position-absolute top-0 start-0 w-100 h-100 d-flex align-items-center justify-content-center bg-dark bg-opacity-75 text-white">
<h2 class="fw-bold text-uppercase">SOLD</h2>
</div>
<?php endif; ?>
</div>
<div class="card-body d-flex flex-column">
<h5 class="card-title"><?= htmlspecialchars($car['make'] . ' ' . $car['model']) ?></h5>
<p class="card-text text-muted"><i class="bi bi-geo-alt-fill me-1"></i> <?= htmlspecialchars($car['city'] . ', ' . $car['province']) ?></p>
<div class="d-flex justify-content-between text-muted small mb-3">
<span><i class="bi bi-calendar me-1"></i> <?= htmlspecialchars($car['year']) ?></span>
<span><i class="bi bi-speedometer2 me-1"></i> <?= number_format($car['mileage']) ?> km</span>
</div>
<h4 class="mt-auto mb-3 text-end fw-bold" style="color: var(--bs-primary);">$<?= number_format($car['price']) ?></h4>
<a href="car_detail.php?id=<?= $car['id'] ?>" class="btn btn-primary <?= ($car['status'] === 'sold') ? 'disabled' : '' ?>">View Details</a>
</div>
</div>
</div>
<?php endforeach; ?>
<?php else: ?>
<div class="col-12">
<div class="alert alert-warning text-center" role="alert">
<h4 class="alert-heading">No Cars Found!</h4>
<p>Your search did not match any of our vehicles. Try adjusting your filters or check back later.</p>
<hr>
<a href="car_list.php" class="btn btn-dark">Clear Filters and See All Cars</a>
</div>
</div>
<?php endif; ?>
</div>
</div>
</div>
</main>
<?php include 'partials/footer.php'; ?>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>