Retrieve from wordpress database with sort

78 Views Asked by At

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>';

}
1

There are 1 best solutions below

1
Rene Pot On

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