prepare(' SELECT t.name FROM teams t JOIN match_teams mt ON t.id = mt.team_id WHERE mt.match_id = ? '); $stmt->execute([$match['id']]); $teams = $stmt->fetchAll(PDO::FETCH_COLUMN); $match_date = new DateTime($match['match_datetime']); $formatted_date = $match_date->format('D, M j, Y '); $formatted_time = $match_date->format('g:i A'); $teams_html = ''; if (count($teams) > 0) { $teams_html = '

Teams: ' . htmlspecialchars(implode(', ', $teams)) . '

'; } return '
' . htmlspecialchars($match['match_type']) . '

' . htmlspecialchars($match['location']) . '

' . $formatted_date . ' at ' . $formatted_time . '

' . $teams_html . 'View Details
'; } header('Content-Type: application/json'); $action = $_GET['action'] ?? ''; switch ($action) { case 'create': $pdo = db(); try { $pdo->beginTransaction(); // Insert match $stmt = $pdo->prepare("INSERT INTO matches (match_datetime, location, match_type) VALUES (?, ?, ?)"); $stmt->execute([ $_POST['match_datetime'], $_POST['location'], $_POST['match_type'] ]); $match_id = $pdo->lastInsertId(); // Handle teams $teams = $_POST['teams'] ?? []; foreach ($teams as $team_input) { $team_id = null; if (is_numeric($team_input)) { // Existing team $team_id = (int)$team_input; } else { // New team, check if it exists $stmt = $pdo->prepare("SELECT id FROM teams WHERE name = ?"); $stmt->execute([$team_input]); $existing_team = $stmt->fetch(); if ($existing_team) { $team_id = $existing_team['id']; } else { // Insert new team $stmt = $pdo->prepare("INSERT INTO teams (name) VALUES (?)"); $stmt->execute([$team_input]); $team_id = $pdo->lastInsertId(); } } if ($team_id) { $stmt = $pdo->prepare("INSERT INTO match_teams (match_id, team_id) VALUES (?, ?)"); $stmt->execute([$match_id, $team_id]); } } $pdo->commit(); echo json_encode(['success' => true]); } catch (PDOException $e) { $pdo->rollBack(); echo json_encode(['success' => false, 'error' => $e->getMessage()]); } break; case 'load_more': $section = $_GET['section'] ?? ''; $offset = (int)($_GET['offset'] ?? 0); $limit = 10; $sql = ''; switch ($section) { case 'upcoming': $sql = "SELECT * FROM matches WHERE match_datetime > NOW() ORDER BY match_datetime ASC LIMIT ? OFFSET ?"; break; case 'recent': $sql = "SELECT * FROM matches WHERE match_datetime <= NOW() AND match_datetime >= NOW() - INTERVAL 2 WEEK ORDER BY match_datetime DESC LIMIT ? OFFSET ?"; break; case 'past': $sql = "SELECT * FROM matches WHERE match_datetime < NOW() - INTERVAL 2 WEEK ORDER BY match_datetime DESC LIMIT ? OFFSET ?"; break; } if ($sql) { $pdo = db(); $stmt = $pdo->prepare($sql); $load_limit = $limit + 1; $stmt->bindParam(1, $load_limit, PDO::PARAM_INT); $stmt->bindParam(2, $offset, PDO::PARAM_INT); $stmt->execute(); $matches = $stmt->fetchAll(); $has_more = count($matches) > $limit; if ($has_more) { array_pop($matches); // Remove the extra match } $rendered_matches = []; foreach($matches as $match) { $rendered_matches[] = render_match_card($match); } echo json_encode(['matches' => $rendered_matches, 'has_more' => $has_more]); } else { echo json_encode(['matches' => [], 'has_more' => false]); } break; case 'get': try { $pdo = db(); $stmt = $pdo->prepare("SELECT * FROM matches WHERE id = ?"); $stmt->execute([$_GET['id']]); $match = $stmt->fetch(PDO::FETCH_ASSOC); $stmt = $pdo->prepare("SELECT team_id FROM match_teams WHERE match_id = ?"); $stmt->execute([$_GET['id']]); $teams = $stmt->fetchAll(PDO::FETCH_COLUMN); $match['teams'] = $teams; echo json_encode(['success' => true, 'match' => $match]); } catch (PDOException $e) { echo json_encode(['success' => false, 'error' => $e->getMessage()]); } break; case 'update': $pdo = db(); try { $pdo->beginTransaction(); // Update match $stmt = $pdo->prepare("UPDATE matches SET match_datetime = ?, location = ?, match_type = ? WHERE id = ?"); $stmt->execute([ $_POST['match_datetime'], $_POST['location'], $_POST['match_type'], $_POST['match_id'] ]); $match_id = $_POST['match_id']; // Delete old team associations $stmt = $pdo->prepare("DELETE FROM match_teams WHERE match_id = ?"); $stmt->execute([$match_id]); // Handle teams $teams = $_POST['teams'] ?? []; foreach ($teams as $team_input) { $team_id = null; if (is_numeric($team_input)) { // Existing team $team_id = (int)$team_input; } else { // New team, check if it exists $stmt = $pdo->prepare("SELECT id FROM teams WHERE name = ?"); $stmt->execute([$team_input]); $existing_team = $stmt->fetch(); if ($existing_team) { $team_id = $existing_team['id']; } else { // Insert new team $stmt = $pdo->prepare("INSERT INTO teams (name) VALUES (?)"); $stmt->execute([$team_input]); $team_id = $pdo->lastInsertId(); } } if ($team_id) { $stmt = $pdo->prepare("INSERT INTO match_teams (match_id, team_id) VALUES (?, ?)"); $stmt->execute([$match_id, $team_id]); } } $pdo->commit(); echo json_encode(['success' => true]); } catch (PDOException $e) { $pdo->rollBack(); echo json_encode(['success' => false, 'error' => $e->getMessage()]); } break; case 'delete': try { $pdo = db(); $stmt = $pdo->prepare("DELETE FROM matches WHERE id = ?"); $stmt->execute([$_GET['id']]); echo json_encode(['success' => true]); } catch (PDOException $e) { echo json_encode(['success' => false, 'error' => $e->getMessage()]); } break; default: echo json_encode(['success' => false, 'error' => 'Invalid action']); break; }