38474-vm/marketplace.php
2026-02-18 08:32:50 +00:00

102 lines
3.6 KiB
PHP

<?php
// marketplace.php
require_once 'includes/auth.php';
require_once 'includes/header.php';
global $pdo;
// Fetch unique filter values
try {
$brands = $pdo->query("SELECT DISTINCT brand FROM cars ORDER BY brand")->fetchAll(PDO::FETCH_COLUMN);
$years = $pdo->query("SELECT DISTINCT year FROM cars ORDER BY year DESC")->fetchAll(PDO::FETCH_COLUMN);
} catch (Exception $e) {
$brands = [];
$years = [];
}
// Build Query
$where = ["status = 'available'"];
$params = [];
if (!empty($_GET['brand'])) {
$where[] = "brand = ?";
$params[] = $_GET['brand'];
}
if (!empty($_GET['year'])) {
$where[] = "year = ?";
$params[] = $_GET['year'];
}
if (!empty($_GET['max_price'])) {
$where[] = "price <= ?";
$params[] = $_GET['max_price'];
}
$sql = "SELECT * FROM cars WHERE " . implode(" AND ", $where) . " ORDER BY created_at DESC";
try {
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$cars = $stmt->fetchAll();
} catch (Exception $e) {
$cars = [];
}
?>
<div class="container mt-5">
<h1>Marketplace</h1>
<p class="mb-5">Browse our premium selection of vehicles.</p>
<!-- Filter Bar -->
<form method="GET" class="filter-bar">
<select name="brand">
<option value="">All Brands</option>
<?php foreach ($brands as $b): ?>
<option value="<?= htmlspecialchars($b) ?>" <?= (isset($_GET['brand']) && $_GET['brand'] == $b) ? 'selected' : '' ?>>
<?= htmlspecialchars($b) ?>
</option>
<?php endforeach; ?>
</select>
<select name="year">
<option value="">All Years</option>
<?php foreach ($years as $y): ?>
<option value="<?= htmlspecialchars($y) ?>" <?= (isset($_GET['year']) && $_GET['year'] == $y) ? 'selected' : '' ?>>
<?= htmlspecialchars($y) ?>
</option>
<?php endforeach; ?>
</select>
<input type="number" name="max_price" placeholder="Max Price" value="<?= htmlspecialchars($_GET['max_price'] ?? '') ?>">
<button type="submit" class="btn">Filter</button>
<a href="marketplace.php" class="btn btn-outline" style="border: none; color: white;">Reset</a>
</form>
<div class="grid">
<?php if (count($cars) > 0): ?>
<?php foreach ($cars as $car): ?>
<div class="card">
<img src="<?= htmlspecialchars($car['image_path'] ?? $car['image_url'] ?? '') ?>" alt="<?= htmlspecialchars($car['brand']) ?>">
<div class="card-body">
<h3 class="card-title">
<?= htmlspecialchars($car['brand'] . ' ' . $car['model']) ?>
<?php if ($car['is_featured']): ?>
<span class="badge" style="float: right;">Featured</span>
<?php endif; ?>
</h3>
<div class="card-price">$<?= number_format((float)$car['price']) ?></div>
<div class="card-meta">
<?= $car['year'] ?> • <?= number_format((float)$car['mileage']) ?> km • <?= $car['fuel_type'] ?>
</div>
<div class="card-actions">
<a href="car_detail.php?id=<?= $car['id'] ?>" class="btn btn-outline" style="width:100%">View Details</a>
</div>
</div>
</div>
<?php endforeach; ?>
<?php else: ?>
<p>No cars found matching your criteria.</p>
<?php endif; ?>
</div>
</div>
<?php require_once 'includes/footer.php'; ?>