This commit is contained in:
Flatlogic Bot 2025-12-11 19:54:08 +00:00
parent 02be882af7
commit adfbb6f750
3 changed files with 104 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

View File

@ -1,4 +1,6 @@
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
require_once 'db/config.php'; // Include the database configuration

View File

@ -3,6 +3,71 @@ session_start();
require_once 'db/config.php'; // Include the database configuration
// Handle CSV download
if (isset($_GET['download_csv'])) {
$user_id = $_SESSION['user_id'];
$pdo = db();
$query = "SELECT name, description, price, location, status, creation_date FROM items WHERE owner_id = ?";
$query_params = [$user_id];
if (isset($_GET['search']) && !empty($_GET['search'])) {
$searchTerm = '%' . $_GET['search'] . '%';
$query .= " AND (name LIKE ? OR description LIKE ?)";
$query_params[] = $searchTerm;
$query_params[] = $searchTerm;
}
if (isset($_GET['status']) && !empty($_GET['status'])) {
$statusFilter = $_GET['status'];
$query .= " AND status = ?";
$query_params[] = $statusFilter;
}
$orderBy = " ORDER BY creation_date DESC"; // Default sort
if (isset($_GET['sort']) && !empty($_GET['sort'])) {
switch ($_GET['sort']) {
case 'name_asc':
$orderBy = " ORDER BY name ASC";
break;
case 'name_desc':
$orderBy = " ORDER BY name DESC";
break;
case 'price_asc':
$orderBy = " ORDER BY price ASC";
break;
case 'price_desc':
$orderBy = " ORDER BY price DESC";
break;
case 'date_asc':
$orderBy = " ORDER BY creation_date ASC";
break;
case 'date_desc':
$orderBy = " ORDER BY creation_date DESC";
break;
}
}
$query .= $orderBy;
$stmt = $pdo->prepare($query);
$stmt->execute($query_params);
$items_to_export = $stmt->fetchAll(PDO::FETCH_ASSOC);
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="my_listings.csv"');
$output = fopen('php://output', 'w');
if (!empty($items_to_export)) {
fputcsv($output, array_keys($items_to_export[0])); // CSV Header
foreach ($items_to_export as $row) {
fputcsv($output, $row);
}
}
fclose($output);
exit();
}
// Pagination constants
$items_per_page = isset($_GET['items_per_page']) ? (int)$_GET['items_per_page'] : 6;
// Validate $items_per_page to be a positive integer, e.g., 6, 12, 24, 48
@ -152,8 +217,45 @@ $items = []; // Initialize an empty array to hold items
</select>
<button class="btn btn-outline-secondary" type="submit">Filter</button>
<a href="my_listings.php" class="btn btn-outline-danger">Clear Filters</a>
<button class="btn btn-outline-success" type="submit" name="download_csv">Download CSV</button>
</div>
</form>
<?php
$activeFilters = [];
if (!empty($_GET['search'])) {
$activeFilters[] = 'Search: <strong>' . htmlspecialchars($_GET['search']) . '</strong>';
}
if (!empty($_GET['status'])) {
$statusText = [
'available' => 'Available',
'rented' => 'Rented',
'unavailable' => 'Unavailable'
][$_GET['status']] ?? ucfirst($_GET['status']);
$activeFilters[] = 'Status: <strong>' . htmlspecialchars($statusText) . '</strong>';
}
if (!empty($_GET['sort'])) {
$sortText = [
'name_asc' => 'Name (A-Z)',
'name_desc' => 'Name (Z-A)',
'price_asc' => 'Price (Low to High)',
'price_desc' => 'Price (High to Low)',
'date_desc' => 'Date Added (Newest)',
'date_asc' => 'Date Added (Oldest)'
][$_GET['sort']] ?? '';
if (!empty($sortText)) {
$activeFilters[] = 'Sort By: <strong>' . htmlspecialchars($sortText) . '</strong>';
}
}
if (!empty($_GET['items_per_page']) && (int)$_GET['items_per_page'] !== 6) { // Assume 6 is default
$activeFilters[] = 'Items per page: <strong>' . htmlspecialchars($_GET['items_per_page']) . '</strong>';
}
if (!empty($activeFilters)):
?>
<div class="alert alert-info mb-4" role="alert">
<strong>Active Filters:</strong> <?php echo implode(' | ', $activeFilters); ?>
</div>
<?php endif; ?>
<?php if (empty($items)): ?>
<p>You have not listed any items yet. <a href="add_item.php">Add a new item</a>.</p>