query("SELECT * FROM categories ORDER BY name"); $categories = $category_stmt->fetchAll(); // Filtering logic $selected_categories = isset($_GET['categories']) && is_array($_GET['categories']) ? $_GET['categories'] : []; $show_favorites = isset($_GET['favorites']) && $_GET['favorites'] == '1'; $sql = "SELECT d.*, (uf.id IS NOT NULL) as is_favorite FROM drills d LEFT JOIN user_favorites uf ON d.id = uf.drill_id AND uf.user_id = ?"; $params = [$current_user_id]; $where_clauses = ["d.coach_id = ?"]; $params[] = $current_user_id; if (!empty($selected_categories)) { $sql .= " JOIN drill_categories dc ON d.id = dc.drill_id"; $where_clauses[] = "dc.category_id IN (" . str_repeat('?,', count($selected_categories) - 1) . "?)"; $params = array_merge($params, $selected_categories); } if ($show_favorites) { $where_clauses[] = "uf.id IS NOT NULL"; } if (!empty($where_clauses)) { $sql .= " WHERE " . implode(' AND ', $where_clauses); } $sql .= " GROUP BY d.id ORDER BY d.created_at DESC"; $stmt = $pdo->prepare($sql); $stmt->execute($params); $drills = $stmt->fetchAll(); } catch (PDOException $e) { // In a real app, log this error. error_log("Database error: " . $e->getMessage()); } ?> My Drills Here you can manage all the drills you have created. Create New Drill Filter By Category > By Favorites > Show only favorites Filter Clear You haven't created any drills yet. Why not start now? Create Your First Drill ...
Here you can manage all the drills you have created.
You haven't created any drills yet.
Why not start now?
...