Author Topic: Sorting After Query  (Read 387 times)

0 Members and 1 Guest are viewing this topic.

Offline piznacTopic starter

  • Enthusiast
    • View Profile
Sorting After Query
« on: August 10, 2007, 04:22:17 PM »
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"

Code: [Select]
<?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\">&nbsp;<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>";
   
 
 } 
?>
PHP Version 4.4.4
MySQL Version 4.1.21-standard
Apache Version 1.3.37(Unix)

Offline lemmin

  • Addict
    • View Profile
Re: Sorting After Query
« Reply #1 on: August 10, 2007, 04:32:40 PM »
did you try the SORT clause?
Code: [Select]
SORT BY SUM(kills)

Something like that. I think that works, if it doesn't, do SUM(kills) as something, then sort by something.

Offline l3asturd

  • Irregular
    • View Profile
Re: Sorting After Query
« Reply #2 on: August 10, 2007, 04:42:02 PM »
here's how I would do it:

Code: [Select]
<?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.

Offline piznacTopic starter

  • Enthusiast
    • View Profile
Re: Sorting After Query
« Reply #3 on: August 10, 2007, 04:43:10 PM »
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?
PHP Version 4.4.4
MySQL Version 4.1.21-standard
Apache Version 1.3.37(Unix)

Offline piznacTopic starter

  • Enthusiast
    • View Profile
Re: Sorting After Query
« Reply #4 on: August 10, 2007, 04:44:23 PM »
Yeah l3asturd Im trying to do it after the query  ;D
PHP Version 4.4.4
MySQL Version 4.1.21-standard
Apache Version 1.3.37(Unix)

Offline piznacTopic starter

  • Enthusiast
    • View Profile
Re: Sorting After Query
« Reply #5 on: August 11, 2007, 03:49:32 PM »
bump
PHP Version 4.4.4
MySQL Version 4.1.21-standard
Apache Version 1.3.37(Unix)

PHP Freaks Forums

« on: »

Tired of these ads? Purchase a supporter subscription to get rid of them.