0

I have a wordpress website, and I'm trying to get the results from a table in alphabetical order (Greek language). I have tried asort, but it does not work. I suppose I am missing something, but I cannot figure it out. Here is the code:

if($homeplayers) {
    asort($homeplayers);
    $i = 1; foreach ($homeplayers as $homeplayer) {
        $output .= '<tr>';
        $output .= '<td style="vertical-align:top;word-wrap:break-word;">';
        $output .= leagueengine_fetch_player_emblem($homeplayer->player_id).leagueengine_fetch_data_from_id($homeplayer->player_id,'data_value' );
        $output .= '</td>';
        $output .= '<td style="text-align:center;vertical-align:top;">';
        if(isset($_POST['import_last_home_lineups']) && in_array($homeplayer->player_id, $home_app)) {      
            $output .= '<input class="homeplayers" name="homeplayers[]" type="checkbox" checked="checked" value="'.$homeplayer->player_id.'">';
        } else {
            $output .= '<input class="homeplayers" name="homeplayers[]" type="checkbox" '.leagueengine_isplaying_tournament($tournament_id,$match_id,$homeplayer->player_id).' value="'.$homeplayer->player_id.'">';
        }
        $output .= '</td>';                 
        $i++;
    }
    $output .= '<td></td>';
    $output .= '</tr>';

}
enigma
  • 3,476
  • 2
  • 17
  • 30
  • @RenePot he is trying to sort the data of `$homeplayers` i guess. @GiwrgosRad please share the query from where you fetch `$homeplayers`. You need to sort your data from that query itself. – Harsh Makani Oct 01 '15 at 14:30
  • @HarshMakani, the code i am using to get them is `function leagueengine_tournament_match_lineups($tournament_id,$match_id) { global $wpdb; $output = ''; $home_team_id = leagueengine_match_info('tournament_match',NULL,NULL,$tournament_id,$match_id,'home_team_id'); $table = $wpdb->prefix . 'leagueengine_player_careers'; $homeplayers = $wpdb->get_results("SELECT * FROM $table WHERE tournament_id = '$tournament_id' AND team_id = '$home_team_id'");` – Giwrgos Rad Oct 02 '15 at 16:19

1 Answers1

0

You're trying to sort an object, with asort, but that function has no clue how you want it sorted.... you need a custom sorting function

usort($homeplayers, "sorter");

function sorter($a,$b){
    // assuming ->player_name is the correct variable
    return strcmp($a->player_name, $b->player_name);
}

Sources: other question and php docs

Community
  • 1
  • 1
Rene Pot
  • 24,681
  • 7
  • 68
  • 92
  • i tried this but did not work 'if($homeplayers) { usort($homeplayers, "sorter"); function sorter($a,$b){ // assuming ->player_name is the correct variable return strcmp($a->player_id,'data_value', $b->player_id,'data_value'); } $i = 1; foreach ($homeplayers as $homeplayer) { $output .= ''; $output .= ''; $output .= leagueengine_fetch_player_emblem($homeplayer->player_id).leagueengine_fetch_data_from_id($homeplayer->player_id,'data_value' ); $output .= '';' – Giwrgos Rad Oct 02 '15 at 17:02