Jump to content

Sort by a math variable inside of a while loop


Greysoul

Recommended Posts

i'm trying to sort by a math variable included in a while loop. obviously if i declare the variable in a loop called from a query...i can't use the order by in the query, or if i can i'm not sure how as i've tried a couple of ways. i've also tried to use sort($kpg) and that didn't quite work as well as i'd hoped. i'm a bit clueless here.

function pkpg(){$score=mysql_query("SELECT * FROM Players WHERE Kills > 0 Limit 0,10");echo "<table>";echo "<tr><th class='th'>Rank</th><th class='th'>Pilot</th><th class='th'>Clan</th><th class='th'>GP</th><th class='th'>KPG</th></tr>";while($ps=mysql_fetch_array($score)){echo "<tr>";echo "<td class='tdp'>" . ++$i . "</td>";echo "<td class='tdp'>" . $ps['Player'] . "</td>";echo "<td class='tdp'>" . $ps['ClanName'] . "</td>";echo "<td class='tdp'>" . $ps['GP'] . "</td>";$kpg= $ps['Kills'] / $ps['GP'];echo "<td class='tdp'>" . $kpg . "</td>";echo "</tr>";}echo "</table>";echo "<br>";}

Link to comment
Share on other sites

I think you can just perform the mathematical operation in your query, assign it an alias and order by that.

$score=mysql_query("SELECT *, (Kills/GP) AS kpg FROM Players WHERE Kills > 0 Limit 0,10 ORDER BY kpg");

That should work, I think.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...