Jump to content

Sorting After Query


piznac

Recommended Posts

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>";

	 } 
?>

Link to comment
https://forums.phpfreaks.com/topic/64314-sorting-after-query/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/64314-sorting-after-query/#findComment-320649
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/64314-sorting-after-query/#findComment-320651
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.