128 lines
6.0 KiB
PHP
128 lines
6.0 KiB
PHP
<?php
|
|
$page_title = "Browse Cars - AFG CARS";
|
|
include 'includes/header.php';
|
|
|
|
$pdo = db();
|
|
|
|
// Filter parameters
|
|
$search = $_GET['q'] ?? '';
|
|
$location = $_GET['location'] ?? '';
|
|
$max_price = $_GET['max_price'] ?? '';
|
|
$sort = $_GET['sort'] ?? 'newest';
|
|
|
|
// Only show approved cars
|
|
$sql = "SELECT * FROM cars WHERE status = 'approved'";
|
|
$params = [];
|
|
|
|
if ($search) {
|
|
$sql .= " AND (title LIKE ? OR brand LIKE ? OR model LIKE ?)";
|
|
$params[] = "%$search%";
|
|
$params[] = "%$search%";
|
|
$params[] = "%$search%";
|
|
}
|
|
|
|
if ($location && $location !== 'All Locations') {
|
|
$sql .= " AND location = ?";
|
|
$params[] = $location;
|
|
}
|
|
|
|
if ($max_price) {
|
|
$sql .= " AND price <= ?";
|
|
$params[] = $max_price;
|
|
}
|
|
|
|
switch ($sort) {
|
|
case 'price_low': $sql .= " ORDER BY price ASC"; break;
|
|
case 'price_high': $sql .= " ORDER BY price DESC"; break;
|
|
case 'oldest': $sql .= " ORDER BY year ASC"; break;
|
|
default: $sql .= " ORDER BY created_at DESC"; break;
|
|
}
|
|
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute($params);
|
|
$cars = $stmt->fetchAll();
|
|
?>
|
|
|
|
<div class="container py-5">
|
|
<div class="row g-4">
|
|
<!-- Sidebar Filters -->
|
|
<div class="col-lg-3">
|
|
<div class="card border-0 shadow-sm p-4 sticky-top" style="top: 100px; border-radius: 20px;">
|
|
<h5 class="fw-bold mb-4">Filters</h5>
|
|
<form method="GET">
|
|
<div class="mb-3">
|
|
<label class="form-label small fw-bold">Search</label>
|
|
<input type="text" name="q" class="form-control" placeholder="Brand, model..." value="<?php echo htmlspecialchars($search); ?>">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label small fw-bold">Location</label>
|
|
<select name="location" class="form-select">
|
|
<option>All Locations</option>
|
|
<option>Kabul</option><option>Herat</option><option>Mazar-i-Sharif</option><option>Kandahar</option>
|
|
</select>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label small fw-bold">Max Price ($)</label>
|
|
<input type="number" name="max_price" class="form-control" placeholder="Any" value="<?php echo htmlspecialchars($max_price); ?>">
|
|
</div>
|
|
<div class="mb-4">
|
|
<label class="form-label small fw-bold">Sort By</label>
|
|
<select name="sort" class="form-select">
|
|
<option value="newest" <?php echo $sort == 'newest' ? 'selected' : ''; ?>>Newest First</option>
|
|
<option value="price_low" <?php echo $sort == 'price_low' ? 'selected' : ''; ?>>Price: Low to High</option>
|
|
<option value="price_high" <?php echo $sort == 'price_high' ? 'selected' : ''; ?>>Price: High to Low</option>
|
|
</select>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary w-100">Apply Filters</button>
|
|
<a href="<?php echo APP_URL; ?>cars.php" class="btn btn-link w-100 mt-2 text-decoration-none text-muted small">Clear All</a>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Car Listings -->
|
|
<div class="col-lg-9">
|
|
<div class="d-flex justify-content-between align-items-center mb-4">
|
|
<h4 class="fw-bold mb-0"><?php echo count($cars); ?> Approved Vehicles</h4>
|
|
</div>
|
|
|
|
<div class="row g-4">
|
|
<?php if (empty($cars)): ?>
|
|
<div class="col-12 text-center py-5">
|
|
<h5>No cars matching your criteria</h5>
|
|
<p class="text-muted">Try adjusting your filters or search terms.</p>
|
|
</div>
|
|
<?php else: ?>
|
|
<?php foreach ($cars as $car): ?>
|
|
<div class="col-md-6 col-xl-4">
|
|
<div class="car-card bg-white h-100 d-flex flex-column position-relative">
|
|
<?php if ($car['badge']): ?>
|
|
<div class="badge bg-warning text-dark position-absolute top-0 start-0 m-3 z-3 shadow-sm"><?php echo htmlspecialchars($car['badge']); ?></div>
|
|
<?php endif; ?>
|
|
|
|
<div class="position-relative">
|
|
<img src="<?php echo htmlspecialchars($car['image']); ?>" class="card-img-top" alt="...">
|
|
<div class="position-absolute top-0 end-0 m-3 z-3">
|
|
<span class="badge bg-white text-dark shadow-sm border-0 py-2 px-3"><?php echo $car['year']; ?></span>
|
|
</div>
|
|
</div>
|
|
<div class="card-body p-4 flex-grow-1 d-flex flex-column">
|
|
<h5 class="fw-bold mb-2"><?php echo htmlspecialchars($car['title']); ?></h5>
|
|
<div class="d-flex align-items-center gap-3 text-muted small mb-4">
|
|
<span><?php echo htmlspecialchars($car['location']); ?></span>
|
|
<span><?php echo number_format($car['mileage']); ?> km</span>
|
|
</div>
|
|
<div class="mt-auto d-flex justify-content-between align-items-center">
|
|
<span class="price-tag">$<?php echo number_format($car['price']); ?></span>
|
|
<a href="<?php echo APP_URL; ?>car-details.php?id=<?php echo $car['id']; ?>" class="btn btn-primary btn-sm px-4">Details</a>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php include 'includes/footer.php'; ?>
|