172 lines
7.7 KiB
PHP
172 lines
7.7 KiB
PHP
<?php
|
|
$title = "Marketplace";
|
|
require_once __DIR__ . '/includes/header.php';
|
|
|
|
// Pagination Logic
|
|
$limit = 6;
|
|
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
|
|
$offset = ($page - 1) * $limit;
|
|
|
|
// Filters
|
|
$search = $_GET['search'] ?? '';
|
|
$brand = $_GET['brand'] ?? '';
|
|
$city = $_GET['city'] ?? '';
|
|
$sort = $_GET['sort'] ?? 'newest';
|
|
|
|
// Build Query
|
|
$query = "SELECT * FROM cars WHERE is_deleted = 0";
|
|
$params = [];
|
|
|
|
if ($search) {
|
|
$query .= " AND (title LIKE ? OR brand LIKE ? OR model LIKE ?)";
|
|
$params[] = "%$search%";
|
|
$params[] = "%$search%";
|
|
$params[] = "%$search%";
|
|
}
|
|
|
|
if ($brand) {
|
|
$query .= " AND brand = ?";
|
|
$params[] = $brand;
|
|
}
|
|
|
|
if ($city) {
|
|
$query .= " AND city = ?";
|
|
$params[] = $city;
|
|
}
|
|
|
|
// Sorting
|
|
switch ($sort) {
|
|
case 'price_low': $query .= " ORDER BY price ASC"; break;
|
|
case 'price_high': $query .= " ORDER BY price DESC"; break;
|
|
case 'oldest': $query .= " ORDER BY created_at ASC"; break;
|
|
default: $query .= " ORDER BY created_at DESC"; break;
|
|
}
|
|
|
|
// Total count for pagination
|
|
$db = db();
|
|
$totalStmt = $db->prepare($query);
|
|
$totalStmt->execute($params);
|
|
$totalRows = $totalStmt->rowCount();
|
|
$totalPages = ceil($totalRows / $limit);
|
|
|
|
// Final query with limit
|
|
$query .= " LIMIT $limit OFFSET $offset";
|
|
$stmt = $db->prepare($query);
|
|
$stmt->execute($params);
|
|
$cars = $stmt->fetchAll();
|
|
|
|
// Get unique brands for filter
|
|
$brandsStmt = $db->query("SELECT DISTINCT brand FROM cars WHERE is_deleted = 0 ORDER BY brand");
|
|
$allBrands = $brandsStmt->fetchAll(PDO::FETCH_COLUMN);
|
|
?>
|
|
|
|
<section>
|
|
<div class="container">
|
|
<div class="section-header">
|
|
<h1 class="text-gradient">Premium Marketplace</h1>
|
|
<p>Browse our curated collection of luxury vehicles from verified sellers across the region.</p>
|
|
</div>
|
|
|
|
<!-- Filters Section -->
|
|
<div class="glass-card filters" style="margin-bottom: 4rem;">
|
|
<form action="cars.php" method="GET" class="filter-row">
|
|
<div class="form-group">
|
|
<label>Search</label>
|
|
<input type="text" name="search" value="<?php echo htmlspecialchars($search); ?>" placeholder="E.g. Land Cruiser" class="form-control">
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label>Brand</label>
|
|
<select name="brand" class="form-control">
|
|
<option value="">All Brands</option>
|
|
<?php foreach ($allBrands as $b): ?>
|
|
<option value="<?php echo $b; ?>" <?php echo $brand == $b ? 'selected' : ''; ?>><?php echo $b; ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label>Location</label>
|
|
<select name="city" class="form-control">
|
|
<option value="">All Locations</option>
|
|
<option value="Kabul" <?php echo $city == 'Kabul' ? 'selected' : ''; ?>>Kabul</option>
|
|
<option value="Herat" <?php echo $city == 'Herat' ? 'selected' : ''; ?>>Herat</option>
|
|
<option value="Mazar-i-Sharif" <?php echo $city == 'Mazar-i-Sharif' ? 'selected' : ''; ?>>Mazar-i-Sharif</option>
|
|
<option value="Kandahar" <?php echo $city == 'Kandahar' ? 'selected' : ''; ?>>Kandahar</option>
|
|
</select>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label>Sort By</label>
|
|
<select name="sort" class="form-control">
|
|
<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>
|
|
|
|
<div class="form-group" style="display: flex; gap: 0.75rem;">
|
|
<button type="submit" class="btn btn-primary">Apply</button>
|
|
<a href="cars.php" class="btn btn-outline">Clear</a>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
|
|
<!-- Cars Grid -->
|
|
<div class="cars-grid">
|
|
<?php if (empty($cars)): ?>
|
|
<div style="grid-column: 1 / -1; text-align: center; padding: 6rem;" class="glass-card">
|
|
<p style="color: var(--text-muted); font-size: 1.25rem;">No vehicles found matching your criteria.</p>
|
|
<a href="cars.php" class="btn btn-primary" style="margin-top: 2rem;">Reset All Filters</a>
|
|
</div>
|
|
<?php else: ?>
|
|
<?php foreach ($cars as $car): ?>
|
|
<a href="car_detail.php?id=<?php echo $car['id']; ?>" class="glass-card car-card">
|
|
<div class="car-image">
|
|
<?php if ($car['is_hot_deal']): ?>
|
|
<span class="badge badge-hot">HOT DEAL</span>
|
|
<?php endif; ?>
|
|
<?php if ($car['status'] == 'SOLD'): ?>
|
|
<span class="badge badge-sold">SOLD</span>
|
|
<?php elseif ($car['created_at'] > date('Y-m-d H:i:s', strtotime('-7 days'))): ?>
|
|
<span class="badge badge-new">NEW LISTING</span>
|
|
<?php endif; ?>
|
|
|
|
<?php
|
|
$imgStmt = $db->prepare("SELECT image_path FROM car_images WHERE car_id = ? AND is_main = 1 LIMIT 1");
|
|
$imgStmt->execute([$car['id']]);
|
|
$mainImg = $imgStmt->fetchColumn();
|
|
$displayImg = $mainImg ?: 'assets/images/pexels/car_1.jpg';
|
|
?>
|
|
<img src="<?php echo htmlspecialchars($displayImg); ?>" alt="<?php echo htmlspecialchars($car['title']); ?>">
|
|
</div>
|
|
<div class="car-info">
|
|
<div class="car-price">$<?php echo number_format($car['price'], 0); ?></div>
|
|
<h3 style="margin-bottom: 0.5rem;"><?php echo htmlspecialchars($car['title']); ?></h3>
|
|
<p style="color: var(--text-muted); margin-bottom: 0; font-size: 0.9rem;"><?php echo htmlspecialchars($car['brand'] . ' ' . $car['model'] . ' | ' . $car['year']); ?></p>
|
|
|
|
<div class="car-meta">
|
|
<span>📍 <?php echo htmlspecialchars($car['city']); ?></span>
|
|
<span>🛣️ <?php echo number_format($car['mileage']); ?> km</span>
|
|
</div>
|
|
</div>
|
|
</a>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<!-- Pagination -->
|
|
<?php if ($totalPages > 1): ?>
|
|
<div style="display: flex; justify-content: center; gap: 1rem; margin-top: 5rem;">
|
|
<?php for ($i = 1; $i <= $totalPages; $i++): ?>
|
|
<a href="cars.php?page=<?php echo $i; ?>&search=<?php echo urlencode($search); ?>&brand=<?php echo urlencode($brand); ?>&city=<?php echo urlencode($city); ?>&sort=<?php echo urlencode($sort); ?>"
|
|
class="btn <?php echo $i == $page ? 'btn-primary' : 'btn-outline'; ?>" style="min-width: 50px;">
|
|
<?php echo $i; ?>
|
|
</a>
|
|
<?php endfor; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</section>
|
|
|
|
<?php require_once __DIR__ . '/includes/footer.php'; ?>
|