Jump to content

Sorting arrays?


php_b34st

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:

[code]<?php

//Include required function files
require_once('db.php');

$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;
    $i++;
    
}

sort($ranks);

echo '
    <table align="center" border="1" width="90%>
        <tr>
            <th>Username</th>
            <th>Army Size</th>
            <th>Treasury</th>
            <th>Rank</th>
         </tr>';
$i = 0;
foreach ($ranks as $r => $value) {
    $username = $info[$r][username];
    $rank = $i + 1;
    echo '
        <tr>
            <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>
             </tr>';
        
    $sql = "UPDATE users
           SET rank='$rank'
       WHERE username='$username'";
      
       $result = mysql_query($sql);
      
    $i++;
}

echo '</table>';
?>[/code]

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?
Link to comment
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:
[code]
function desc($a, $b)
{
   if($a['rank'] == $b['rank'])
      return 0;
   else
      return ($a['rank'] > $b['rank']) ? -1 : 1;
}
[/code]

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.