// ========================================================================= // SHORTCODE: Rangers Affiliates (Filtered, Paginated, Columned Layout) // ========================================================================= add_shortcode('rangers-affiliates', 'render_custom_rangers_affiliates'); function render_custom_rangers_affiliates() { global $wpdb; $table_name = 'otg_rangers_affiliates'; // 1. Get Filter and Pagination Inputs from GET parameters $filter_start_season = isset($_GET['aff_start']) ? sanitize_text_field($_GET['aff_start']) : ''; $filter_end_season = isset($_GET['aff_end']) ? sanitize_text_field($_GET['aff_end']) : ''; $filter_team = isset($_GET['aff_team']) ? sanitize_text_field($_GET['aff_team']) : ''; $filter_league = isset($_GET['aff_league']) ? sanitize_text_field($_GET['aff_league']) : ''; $paged = isset($_GET['paged']) ? max(1, intval($_GET['paged'])) : 1; $per_page = 25; $offset = ($paged - 1) * $per_page; // Season range validation logic: // If start year is after end year, reset end to start. If end is before start year, reset start/end accordingly. if (!empty($filter_start_season) && !empty($filter_end_season)) { if ($filter_start_season > $filter_end_season) { $filter_end_season = $filter_start_season; } } // Build Dynamic WHERE Clause $where_clauses = []; $params = []; if (!empty($filter_start_season)) { $where_clauses[] = "season >= %s"; $params[] = $filter_start_season; } if (!empty($filter_end_season)) { $where_clauses[] = "season <= %s"; $params[] = $filter_end_season; } if (!empty($filter_team)) { $where_clauses[] = "team_name = %s"; $params[] = $filter_team; } if (!empty($filter_league)) { $where_clauses[] = "league_name = %s"; $params[] = $filter_league; } $where_sql = ''; if (!empty($where_clauses)) { $where_sql = " WHERE " . implode(' AND ', $where_clauses); } // 2. Fetch Unique Teams and Leagues for Dropdown Filter Selects $all_teams = $wpdb->get_col("SELECT DISTINCT team_name FROM $table_name ORDER BY team_name ASC"); $all_leagues = $wpdb->get_col("SELECT DISTINCT league_name FROM $table_name ORDER BY league_name ASC"); // 3. Get Total Records Count for Pagination $count_sql = "SELECT COUNT(*) FROM $table_name" . $where_sql; if (!empty($params)) { $total_records = intval($wpdb->get_var($wpdb->prepare($count_sql, $params))); } else { $total_records = intval($wpdb->get_var($count_sql)); } $total_pages = max(1, ceil($total_records / $per_page)); if ($paged > $total_pages) { $paged = $total_pages; $offset = ($paged - 1) * $per_page; } // 4. Fetch the Paginated Dataset (Sorted by most recent season first) // Using SUBSTRING_INDEX or standard ordering depending on your season format (e.g., "2025-26" or "2025") $sql = "SELECT * FROM $table_name" . $where_sql . " ORDER BY season DESC, id DESC LIMIT %d OFFSET %d"; $query_params = array_merge($params, [$per_page, $offset]); $results = $wpdb->get_results($wpdb->prepare($sql, $query_params)); // Calculate record display range $start_record = ($total_records > 0) ? $offset + 1 : 0; $end_record = min($offset + $per_page, $total_records); $current_url = strtok($_SERVER['URI'] ?? $_SERVER['REQUEST_URI'], '?'); $query_args = $_GET; // Standardized form input style matching your site theme $input_style = 'height: 38px; padding: 0 10px; background: #161616; color: #fff; border: 1px solid #444; border-radius: 4px; box-sizing: border-box; font-size: 0.9em;'; // Start Output Wrapper $output = '
| Season | Team Name | League | Affiliate Details |
|---|---|---|---|
| No affiliate history found matching your criteria. | |||
| ' . esc_html($row->season) . ' | '; $output .= '' . $logo_html . '' . esc_html($row->team_name) . ' | '; $output .= '' . esc_html($row->league_name) . ' | '; $output .= '- | '; // Placeholder or extra context if needed per-row $output .= '