This commit is contained in:
Flatlogic Bot 2025-12-11 19:39:42 +00:00
parent 79aa26cf52
commit ff24217c73

View File

@ -3,6 +3,9 @@ session_start();
require_once 'db/config.php'; // Include the database configuration require_once 'db/config.php'; // Include the database configuration
// Pagination constants
const ITEMS_PER_PAGE = 6;
// If user is not logged in or not a vendor, redirect // If user is not logged in or not a vendor, redirect
if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] !== 'vendor') { if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] !== 'vendor') {
header("Location: login.php"); // Redirect to login or a suitable page header("Location: login.php"); // Redirect to login or a suitable page
@ -17,24 +20,81 @@ $pdo = db(); // Get the PDO connection
$items = []; // Initialize an empty array to hold items $items = []; // Initialize an empty array to hold items
// Fetch items listed by the current vendor // 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 = "SELECT * FROM items WHERE owner_id = ?";
$params = [$user_id]; $query_params = [$user_id];
if (isset($_GET['search']) && !empty($_GET['search'])) { if (isset($_GET['search']) && !empty($_GET['search'])) {
$searchTerm = '%' . $_GET['search'] . '%'; $searchTerm = '%' . $_GET['search'] . '%';
$query .= " AND (name LIKE ? OR description LIKE ?)"; $query .= " AND (name LIKE ? OR description LIKE ?)";
$params[] = $searchTerm; $query_params[] = $searchTerm;
$params[] = $searchTerm; $query_params[] = $searchTerm;
} }
if (isset($_GET['status']) && !empty($_GET['status'])) { if (isset($_GET['status']) && !empty($_GET['status'])) {
$statusFilter = $_GET['status']; $statusFilter = $_GET['status'];
$query .= " AND status = ?"; $query .= " AND status = ?";
$params[] = $statusFilter; $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 = $pdo->prepare($query);
$stmt->execute($params); $stmt->execute($query_params);
$items = $stmt->fetchAll(); $items = $stmt->fetchAll();
?> ?>
<?php include 'header.php'; ?> <?php include 'header.php'; ?>
@ -68,6 +128,15 @@ $items = []; // Initialize an empty array to hold items
<option value="rented" <?php echo (isset($_GET['status']) && $_GET['status'] === 'rented') ? 'selected' : ''; ?>>Rented</option> <option value="rented" <?php echo (isset($_GET['status']) && $_GET['status'] === 'rented') ? 'selected' : ''; ?>>Rented</option>
<option value="unavailable" <?php echo (isset($_GET['status']) && $_GET['status'] === 'unavailable') ? 'selected' : ''; ?>>Unavailable</option> <option value="unavailable" <?php echo (isset($_GET['status']) && $_GET['status'] === 'unavailable') ? 'selected' : ''; ?>>Unavailable</option>
</select> </select>
<select class="form-select" name="sort">
<option value="" <?php echo (!isset($_GET['sort']) || $_GET['sort'] === '') ? 'selected' : ''; ?>>Sort By</option>
<option value="name_asc" <?php echo (isset($_GET['sort']) && $_GET['sort'] === 'name_asc') ? 'selected' : ''; ?>>Name (A-Z)</option>
<option value="name_desc" <?php echo (isset($_GET['sort']) && $_GET['sort'] === 'name_desc') ? 'selected' : ''; ?>>Name (Z-A)</option>
<option value="price_asc" <?php echo (isset($_GET['sort']) && $_GET['sort'] === 'price_asc') ? 'selected' : ''; ?>>Price (Low to High)</option>
<option value="price_desc" <?php echo (isset($_GET['sort']) && $_GET['sort'] === 'price_desc') ? 'selected' : ''; ?>>Price (High to Low)</option>
<option value="date_desc" <?php echo (isset($_GET['sort']) && $_GET['sort'] === 'date_desc') ? 'selected' : ''; ?>>Date Added (Newest)</option>
<option value="date_asc" <?php echo (isset($_GET['sort']) && $_GET['sort'] === 'date_asc') ? 'selected' : ''; ?>>Date Added (Oldest)</option>
</select>
<button class="btn btn-outline-secondary" type="submit">Filter</button> <button class="btn btn-outline-secondary" type="submit">Filter</button>
</div> </div>
</form> </form>
@ -96,6 +165,23 @@ $items = []; // Initialize an empty array to hold items
</div> </div>
<?php endforeach; ?> <?php endforeach; ?>
</div> </div>
<?php if ($total_pages > 1): ?>
<nav aria-label="Page navigation">
<ul class="pagination justify-content-center">
<li class="page-item <?php echo ($current_page <= 1) ? 'disabled' : ''; ?>">
<a class="page-link" href="?<?php echo http_build_query(array_merge($_GET, ['page' => $current_page - 1])); ?>" tabindex="-1" aria-disabled="true">Previous</a>
</li>
<?php for ($i = 1; $i <= $total_pages; $i++): ?>
<li class="page-item <?php echo ($current_page == $i) ? 'active' : ''; ?>"><a class="page-link" href="?<?php echo http_build_query(array_merge($_GET, ['page' => $i])); ?>"><?php echo $i; ?></a></li>
<?php endfor; ?>
<li class="page-item <?php echo ($current_page >= $total_pages) ? 'disabled' : ''; ?>">
<a class="page-link" href="?<?php echo http_build_query(array_merge($_GET, ['page' => $current_page + 1])); ?>">Next</a>
</li>
</ul>
</nav>
<?php endif; ?>
<?php endif; ?> <?php endif; ?>
</div> </div>
</div> </div>