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 if (!in_array($items_per_page, [6, 12, 24, 48])) { $items_per_page = 6; // Default to 6 if invalid value is provided } // Pagination constants // const ITEMS_PER_PAGE = 6; // If user is not logged in or not a vendor, redirect if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] !== 'vendor') { header("Location: login.php"); // Redirect to login or a suitable page exit; } $user_id = $_SESSION['user_id']; $user_name = $_SESSION['user_name']; $pdo = db(); // Get the PDO connection $items = []; // Initialize an empty array to hold items // Fetch items listed by the current vendor $total_items_query = "SELECT COUNT(*) FROM items WHERE owner_id = ?"; $total_items_params = [$user_id]; if (isset($_GET['search']) && !empty($_GET['search'])) { $searchTerm = '%' . $_GET['search'] . '%'; $total_items_query .= " AND (name LIKE ? OR description LIKE ?)"; $total_items_params[] = $searchTerm; $total_items_params[] = $searchTerm; } if (isset($_GET['status']) && !empty($_GET['status'])) { $statusFilter = $_GET['status']; $total_items_query .= " AND status = ?"; $total_items_params[] = $statusFilter; } $total_stmt = $pdo->prepare($total_items_query); $total_stmt->execute($total_items_params); $total_items = $total_stmt->fetchColumn(); $total_pages = ceil($total_items / $items_per_page); $current_page = isset($_GET['page']) ? (int)$_GET['page'] : 1; $current_page = max(1, min($current_page, $total_pages == 0 ? 1 : $total_pages)); // Ensure current page is within valid range $offset = ($current_page - 1) * ITEMS_PER_PAGE; // Fetch items listed by the current vendor $query = "SELECT * 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; $query .= " LIMIT ? OFFSET ?"; $query_params[] = $items_per_page; $query_params[] = $offset; $stmt = $pdo->prepare($query); $stmt->execute($query_params); $items = $stmt->fetchAll(); ?>

's Listings

Manage your listed items here.

Clear Filters
' . htmlspecialchars($_GET['search']) . ''; } if (!empty($_GET['status'])) { $statusText = [ 'available' => 'Available', 'rented' => 'Rented', 'unavailable' => 'Unavailable' ][$_GET['status']] ?? ucfirst($_GET['status']); $activeFilters[] = 'Status: ' . htmlspecialchars($statusText) . ''; } 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: ' . htmlspecialchars($sortText) . ''; } } if (!empty($_GET['items_per_page']) && (int)$_GET['items_per_page'] !== 6) { // Assume 6 is default $activeFilters[] = 'Items per page: ' . htmlspecialchars($_GET['items_per_page']) . ''; } if (!empty($activeFilters)): ?>

You have not listed any items yet. Add a new item.

<?php echo htmlspecialchars($item['name']); ?>

Price: $ per day

Location:

View Edit Delete

Status:

1): ?>