AAC Alphabetical sorting in a loop

Unknown Soldier

Tibian Geodesist
Joined
Oct 30, 2010
Messages
76
Reaction score
70
Hi everyone!


I have a little request for those, who are good at php. I have a subpage where all statistics about number of killed monsters are. It works perfectly fine, however I feel it is a little bit too messy, because the table is not sorted in any way. So I would like to ask you for help in adding alpabetical sorting to the list, so the monsters are sorted from A to Z.

If there were a feature that the table could be sorted descending and ascending, it would be perfect!

Thanks in advance!



killedmonsters.PNG


PHP:
<?php
 
/* Scrip by zonet */
$kills = $config['site']['creatureKills'];
$limit = $config['site']['creatureKillShowLimit'];
$main_content .= '<table border="0px" cellspacing="1px" cellpadding="4px" width="100%">
           <TR BGCOLOR="'.$config['site']['vdarkborder'].'" style="color: white;"><th>Creature</th><th>'.$limit.'  Top Killers</th></tr>';
$row = 1;

    foreach($kills as $name => $storage) {
    $qa = $SQL->query('SELECT `player_storage`.`player_id`, `player_storage`.`key`, `player_storage`.`value` AS `value`, `players`.`id`, `players`.`name` AS `name` FROM `player_storage` LEFT JOIN `players` ON `player_storage`.`player_id` = `players`.`id` WHERE `player_storage`.`key` = '.$storage.' ORDER BY ABS(value) DESC LIMIT '.$limit)->fetchAll();
    $color = ( $row % 2 ? $config['site']['darkborder'] : $config['site']['lightborder']);
    $row++;
    $main_content .= '<tr bgcolor="'.$color.'"><td width="40%" style="font-size: 10pt; font-weight: bold; "><center>'.(ucfirst($name)).'</center></td><td>';
        $a = 0;
        foreach( $qa as $q )
        {
            $a++;

            $main_content .= '<b>'.$a.'.</b> <a href="?subtopic=characters&name='.urlencode($q['name']).'">'.$q['name'].'</a> (Kills:  '.$q['value'].') </font></font></font><br />';
        }
    }
    $main_content .= '</td></table>';
?>
 

Acubens

Old Penguin
Joined
May 6, 2008
Messages
973
Reaction score
70
Location
Venezuela
Like Bogart say, in your script before use foreach, under:

PHP:
$row = 1;
Put this

PHP:
sort($kills) // Order from A to Z
or

PHP:
rsort($kills) // Order reverse from Z to A
 
OP
Unknown Soldier

Unknown Soldier

Tibian Geodesist
Joined
Oct 30, 2010
Messages
76
Reaction score
70
Was it really so easy?

I have solved it outside this script sorting the table content in just an excel, however it's nice to see that solution is simple and my quetion was on really low level... I feel stupid...
Thank you both, I will use sort/rsort function :)
 

Bogart

...has super panda powers
Joined
Jun 21, 2009
Messages
8,010
Reaction score
531
Location
Panda Land!
Sometimes solutions are already in the internet, even if it's not in OTLand, for example if you search "sort php" the first link will give you the same answer I provided
 
Top