36874-vm/listings.php
Flatlogic Bot 1d182fef38 1.1
2025-12-11 19:19:52 +00:00

75 lines
3.2 KiB
PHP

<?php include 'header.php'; ?>
<?php
require_once 'db/config.php';
// Fetch items from the database
try {
$pdo = db();
$location = $_GET['location'] ?? '';
if (!empty($location)) {
$stmt = $pdo->prepare("SELECT * FROM items WHERE location LIKE ? ORDER BY created_at DESC");
$stmt->execute(["%{$location}%"]);
} else {
$stmt = $pdo->query("SELECT * FROM items ORDER BY created_at DESC");
}
$items = $stmt->fetchAll();
} catch (PDOException $e) {
// For a real app, you'd log this error and show a user-friendly message
$items = [];
$db_error = "Database error: " . $e->getMessage();
}
?>
<div class="container section">
<h1 class="section-title">Listings</h1>
<div class="row mb-4">
<div class="col-md-12">
<form class="search-bar" action="listings.php" method="GET">
<div class="input-group">
<input type="text" class="form-control form-control-lg search-input" name="location" placeholder="Filter by city, neighborhood, or address..." value="<?php echo htmlspecialchars($location); ?>">
<button class="btn btn-light btn-lg search-button" type="submit">
<i data-feather="search"></i> Search
</button>
</div>
</form>
</div>
</div>
<?php if (isset($db_error)): ?>
<div class="alert alert-danger"><?php echo $db_error; ?></div>
<?php endif; ?>
<div class="row">
<?php if (empty($items)): ?>
<div class="col">
<div class="alert alert-info">No items found.<?php if (!empty($location)) { echo ' for "' . htmlspecialchars($location) . '"'; } ?>. Try a different search or check back later!</div>
</div>
<?php else: ?>
<?php foreach ($items as $item): ?>
<div class="col-md-6 col-lg-4 mb-4">
<div class="card h-100">
<img src="https://images.pexels.com/photos/276583/pexels-photo-276583.jpeg?auto=compress&cs=tinysrgb&w=600" class="card-img-top" alt="<?php echo htmlspecialchars($item['name']); ?>" style="height: 200px; object-fit: cover;">
<div class="card-body d-flex flex-column">
<h5 class="card-title"><?php echo htmlspecialchars($item['name']); ?></h5>
<p class="card-text text-muted"><?php echo htmlspecialchars($item['location']); ?></p>
<p class="card-text"><?php echo htmlspecialchars(substr($item['description'], 0, 100)); ?>...</p>
<div class="mt-auto d-flex justify-content-between align-items-center">
<strong>$<?php echo htmlspecialchars(number_format($item['price_per_day'], 2)); ?> / day</strong>
<a href="item_details.php?id=<?php echo htmlspecialchars($item['item_id']); ?>" class="btn btn-primary btn-sm">Rent Now</a>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
</div>
<?php include 'footer.php'; ?>