php_b34st Posted March 2, 2006 Share Posted March 2, 2006 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 filesrequire_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? Quote Link to comment Share on other sites More sharing options...
khburres Posted March 2, 2006 Share Posted March 2, 2006 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. Quote Link to comment Share on other sites More sharing options...
php_b34st Posted March 2, 2006 Author Share Posted March 2, 2006 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. Quote Link to comment Share on other sites More sharing options...
khburres Posted March 2, 2006 Share Posted March 2, 2006 You're welcome; glad it worked! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.