Jump to content


This topic is now archived and is closed to further replies.


Sorting arrays?

Recommended Posts

The following script is supposed to work out a users rank, then sort by the highest rank and put the info into a table showing all the users and ranks:


//Include required function files

$query = "SELECT * FROM users";
$result = mysql_query($query) or die("Could not query: " . mysql_error());

$ranks = array();
$info = array();
$i = 0;

while ($row = mysql_fetch_assoc($result)) {
    $army_size = $row['untrained_soldiers'] + $row['attack_soldiers'] + $row['defense_soldiers'];
    $strike = ($row['knife'] * 10) + ($row['revolver'] * 50) + ($row['machine_gun'] * 100);
    $defense = ($row['helmet'] * 10) + ($row['shield'] * 50) + ($row['armor'] * 100);
    $spy = $row['hook'] * 500;
    $sentry = $row['dog'] * 500;
    $rank = ($strike + $defense + $spy + $sentry) / 4;
    $info[$i]['id'] = $row['stats_id'];
    $info[$i]['username'] = $row['username'];
    $info[$i]['army_size'] = $army_size;
    $info[$i]['gold'] = $row['gold'];
    $ranks[$i] = $rank;


echo '
    <table align="center" border="1" width="90%>
            <th>Army Size</th>
$i = 0;
foreach ($ranks as $r => $value) {
    $username = $info[$r][username];
    $rank = $i + 1;
    echo '
            <td><a href=stats.php?id=' . $info[$r][stats_id] . '>' . $info[$r][username] . '</a></td>
                <td>' . $info[$r][army_size] . '</td>
                <td>' . $info[$r][gold] . '</td>
                <td>' . $ranks[$i]. '</td>
        <td>' . $rank. '</td>
    $sql = "UPDATE users
           SET rank='$rank'
       WHERE username='$username'";
       $result = mysql_query($sql);

echo '</table>';

The script is working fine except for when it comes to displaying the ranks in order (highest rank should be top) instead of sorting the ranks it puts the info into the table in the same order that it comes out of the db. Can anyone show me where i have gone wrong?

Share this post

Link to post
Share on other sites
Consider storing your ranks in the $info array, then sort that array based on rank.

Instead of: $ranks[$i] = $rank;
Try: $info[$i]['rank'] = $rank;

Then use a custom function to sort.
Instead of: sort($ranks);
Try: usort($info, "desc");

Somewhere else define the function:
function desc($a, $b)
   if($a['rank'] == $b['rank'])
      return 0;
      return ($a['rank'] > $b['rank']) ? -1 : 1;

Once you call usort, your $info array should be sorted by rank in descending order.
Print out in table as normal. Update db if necessary.

Now, having said that, if you are going to store the rank in the db anyway,
why not use ORDER BY rank DESC? Unless of course the ranking constantly changes.

Share this post

Link to post
Share on other sites
Thanks, that worked exactly how i wanted it to. I have to do it that way instead of ORDER BY rank DESC because the db will be updated hourly. Thanks for the help.

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.