prepare("SELECT * FROM categories WHERE slug = ?"); $stmt->execute([$category_slug]); $category = $stmt->fetch(PDO::FETCH_ASSOC); if (!$category) { die("Category not found."); } $sort = $_GET['sort'] ?? 'popularity'; // New default for categories $filter_brand = $_GET['filter_brand'] ?? ''; $results = []; $brands = []; // Get all products in this category $sql = "SELECT p.id, p.name, p.brand, p.image_url FROM products p WHERE p.category_id = ?"; $params = [$category['id']]; // Get all possible brands for the current category $brand_stmt = $pdo->prepare("SELECT DISTINCT brand FROM products WHERE category_id = ? AND brand IS NOT NULL ORDER BY brand"); $brand_stmt->execute([$category['id']]); $brands = $brand_stmt->fetchAll(PDO::FETCH_COLUMN); // Add brand filter if (!empty($filter_brand)) { $sql .= " AND p.brand = ?"; $params[] = $filter_brand; } // Sorting logic for category page switch ($sort) { case 'price_asc': $sql .= " ORDER BY (SELECT MIN(effective_price) FROM product_listings WHERE product_id = p.id) ASC"; break; case 'price_desc': $sql .= " ORDER BY (SELECT MIN(effective_price) FROM product_listings WHERE product_id = p.id) DESC"; break; case 'rating': $sql .= " ORDER BY (SELECT AVG(rating) FROM product_listings WHERE product_id = p.id) DESC"; break; default: // Popularity/relevance for categories could be based on number of listings or other metrics $sql .= " ORDER BY p.id DESC"; // Simple default: newest products first break; } $stmt = $pdo->prepare($sql); $stmt->execute($params); $products = $stmt->fetchAll(PDO::FETCH_ASSOC); $product_ids = array_column($products, 'id'); if (!empty($product_ids)) { $placeholders = implode(',', array_fill(0, count($product_ids), '?')); $stmt = $pdo->prepare("SELECT *, pl.id as listing_id, plat.name as platform_name FROM product_listings pl JOIN platforms plat ON pl.platform_id = plat.id WHERE pl.product_id IN ($placeholders)"); $stmt->execute($product_ids); $listings = $stmt->fetchAll(PDO::FETCH_ASSOC); $listings_by_product = []; foreach ($listings as $listing) { $listings_by_product[$listing['product_id']][] = $listing; } foreach ($products as $product) { $product['listings'] = $listings_by_product[$product['id']] ?? []; if (!empty($product['listings'])) { $cheapest = array_reduce($product['listings'], fn($c, $i) => ($c === null || $i['effective_price'] < $c['effective_price']) ? $i : $c); $product['cheapest_price'] = $cheapest['effective_price']; $product['badge'] = $cheapest['badge']; } $results[] = $product; } } ?> <?php echo htmlspecialchars($category['name']); ?> - XUPER MALL
← Back to Home

No products found in this category yet.
<?php echo htmlspecialchars($product['name']); ?>

From: $

Available on: