piznac Posted August 10, 2007 Share Posted August 10, 2007 I have a db with intergers to hold "kills" & "deaths". This is for a game,.. now I have pulled my query ,. done my math,. and came to the number I want. Question is how would I sort it by the lowest number at this point? I need it sorted by "$tt" <?php function ratio(){ $ratio_query = "SELECT name, SUM(death), SUM(kills) FROM `stats`,`members` WHERE `stats`.`key` = `members`.`key_hash` GROUP BY `stats`.`key`"; $ratio = mysql_query($ratio_query) or die("Kills query error:" . mysql_error()); $ratio_row = mysql_fetch_assoc($ratio); echo "<table align=\"center\">"; echo "<tr>"; echo "<td colspan=\"2\"> <b>Kill/Death Ratio</b></td>"; echo "</tr>"; do { $name = explode(" ", $ratio_row['key']); foreach($name as $key){ $deaths = $ratio_row['SUM(death)']; $kills = $ratio_row['SUM(kills)']; $name2 = $ratio_row['name']; //if($deaths = 0){ //$deaths = 1;} $r = $kills/$deaths; $t = 1/$r; $tt = substr("$t",0,4); echo "<tr>"; echo "<td>$name2</td><td>$tt-$deaths-$kills</td>"; echo "</tr>";} }while($ratio_row = mysql_fetch_assoc($ratio)); echo "</table>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/64314-sorting-after-query/ Share on other sites More sharing options...
lemmin Posted August 10, 2007 Share Posted August 10, 2007 did you try the SORT clause? SORT BY SUM(kills) Something like that. I think that works, if it doesn't, do SUM(kills) as something, then sort by something. Quote Link to comment https://forums.phpfreaks.com/topic/64314-sorting-after-query/#findComment-320640 Share on other sites More sharing options...
l3asturd Posted August 10, 2007 Share Posted August 10, 2007 here's how I would do it: <?php $ratio_query = "SELECT name, SUM(death) AS TotalDeaths, SUM(kills) AS TotalKills FROM `stats`,`members` WHERE `stats`.`key` = `members`.`key_hash` GROUP BY `stats`.`key` ORDER BY TotalKills ASC"; ?> However, I am a noob, and I doubt this is the result you're looking for. This would result in sorting Total Kills, whereas you're trying to sort the K/D ratio I believe. I'll keep thinking. Quote Link to comment https://forums.phpfreaks.com/topic/64314-sorting-after-query/#findComment-320649 Share on other sites More sharing options...
piznac Posted August 10, 2007 Author Share Posted August 10, 2007 You lost me? SQL? That would be ORDER BY,.. and that would work if I didnt do the calculations after the query. Or is that a php function Im not aware of? I have looked up the "sort" function of arrays,.. but if I explode the results into an array will I then be able to echo them back the way I have? Quote Link to comment https://forums.phpfreaks.com/topic/64314-sorting-after-query/#findComment-320651 Share on other sites More sharing options...
piznac Posted August 10, 2007 Author Share Posted August 10, 2007 Yeah l3asturd Im trying to do it after the query Quote Link to comment https://forums.phpfreaks.com/topic/64314-sorting-after-query/#findComment-320652 Share on other sites More sharing options...
piznac Posted August 11, 2007 Author Share Posted August 11, 2007 bump Quote Link to comment https://forums.phpfreaks.com/topic/64314-sorting-after-query/#findComment-321234 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.