global $wpdb; // 1. Fetch all top 100 players ordered by rank // Replace 'nhl_player_stats' with your actual table name $table_name = 'nhl_player_stats'; $top_100 = $wpdb->get_results( "SELECT * FROM {$table_name} WHERE rank <= 100 ORDER BY rank ASC" ); if ( ! $top_100 ) { return '
No player data found.
'; } // Map the top 100 by rank for quick O(1) lookups $rank_map = []; foreach ( $top_100 as $player ) { $rank_map[intval( $player->rank )] = $player; } // 2. Identify active players within the top 100 and determine their windows (±3) $included_ranks = []; foreach ( $top_100 as $player ) { // Adjust this condition based on how you flag active players in your database if ( isset( $player->is_active ) && $player->is_active == 1 ) { $target_rank = intval( $player->rank ); // Define the window (3 ahead, 3 behind, bounded between 1 and 100) $start = max( 1, $target_rank - 3 ); $end = min( 100, $target_rank + 3 ); for ( $r = $start; $r <= $end; $r++ ) { $included_ranks[$r] = true; } } } // Sort the captured ranks ascending ksort( $included_ranks ); // 3. Build the final display list and check for gaps $players_to_display = []; foreach ( array_keys( $included_ranks ) as $rank ) { if ( isset( $rank_map[$rank] ) ) { $players_to_display[] = $rank_map[$rank]; } } // 4. Render the Output Table $output = '| Rank | Player | Team | Stats |
|---|---|---|---|
| • • • | |||
| ' . esc_html( $player->rank ) . ' | '; $output .= '' . esc_html( $player->player_name ) . ' | '; $output .= '' . esc_html( $player->team_abbr ) . ' | '; $output .= '' . esc_html( $player->points ?? '' ) . ' | '; $output .= '