AAC Alphabetical sorting in a loop

Discussion in 'Support' started by Unknown Soldier, Oct 12, 2018.

  1. Unknown Soldier

    Unknown Soldier Tibian Geodesist

    Joined:
    Oct 30, 2010
    Messages:
    61
    Likes Received:
    54
    Best Answers:
    2
    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:
    1.  
    2. <?php
    3.  
    4. /* Scrip by zonet */
    5. $kills = $config['site']['creatureKills'];
    6. $limit = $config['site']['creatureKillShowLimit'];
    7. $main_content .= '<table border="0px" cellspacing="1px" cellpadding="4px" width="100%">
    8.           <TR BGCOLOR="'.$config['site']['vdarkborder'].'" style="color: white;"><th>Creature</th><th>'.$limit.'  Top Killers</th></tr>';
    9. $row = 1;
    10.  
    11.     foreach($kills as $name => $storage) {
    12.     $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();
    13.     $color = ( $row % 2 ? $config['site']['darkborder'] : $config['site']['lightborder']);
    14.     $row++;
    15.     $main_content .= '<tr bgcolor="'.$color.'"><td width="40%" style="font-size: 10pt; font-weight: bold; "><center>'.(ucfirst($name)).'</center></td><td>';
    16.         $a = 0;
    17.         foreach( $qa as $q )
    18.         {
    19.             $a++;
    20.  
    21.             $main_content .= '<b>'.$a.'.</b> <a href="?subtopic=characters&name='.urlencode($q['name']).'">'.$q['name'].'</a> (Kills:  '.$q['value'].') </font></font></font><br />';
    22.         }
    23.     }
    24.     $main_content .= '</td></table>';
    25. ?>
     
  2. Bogart

    Bogart ...has super panda powers

    Joined:
    Jun 21, 2009
    Messages:
    8,014
    Likes Received:
    551
    Best Answers:
    26
    sort($kills) a-z
    rsort($kills) z-a
     
    Acubens and Unknown Soldier like this.
  3. Acubens

    Acubens Old Penguin

    Joined:
    May 6, 2008
    Messages:
    970
    Likes Received:
    71
    Best Answers:
    1
    Like Bogart say, in your script before use foreach, under:

    PHP:
    1.  
    2. $row = 1;
    3.  
    Put this

    PHP:
    1.  
    2. sort($kills) // Order from A to Z
    3.  
    or

    PHP:
    1.  
    2. rsort($kills) // Order reverse from Z to A
    3.  
     
    Unknown Soldier likes this.
  4. Unknown Soldier

    Unknown Soldier Tibian Geodesist

    Joined:
    Oct 30, 2010
    Messages:
    61
    Likes Received:
    54
    Best Answers:
    2
    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 :)
     
  5. Bogart

    Bogart ...has super panda powers

    Joined:
    Jun 21, 2009
    Messages:
    8,014
    Likes Received:
    551
    Best Answers:
    26
    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
     
    Acubens likes this.

Share This Page

Loading...