Jump to content


Photo

Sorting arrays?


  • Please log in to reply
3 replies to this topic

#1 php_b34st

php_b34st
  • Members
  • PipPipPip
  • Advanced Member
  • 168 posts

Posted 02 March 2006 - 12:35 AM

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:

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

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?

#2 khburres

khburres
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 02 March 2006 - 04:36 PM

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;
   else
      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.
He who guards his mouth and his tongue keeps himself from calamity.

#3 php_b34st

php_b34st
  • Members
  • PipPipPip
  • Advanced Member
  • 168 posts

Posted 02 March 2006 - 05:22 PM

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.

#4 khburres

khburres
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 02 March 2006 - 05:29 PM

You're welcome; glad it worked!
He who guards his mouth and his tongue keeps himself from calamity.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users