Greysoul Posted March 12, 2010 Share Posted March 12, 2010 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 More sharing options...
ShadowMage Posted March 12, 2010 Share Posted March 12, 2010 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 More sharing options...
Greysoul Posted March 12, 2010 Author Share Posted March 12, 2010 looks like the array didn't like that too much, should there be a comma after the *?Warning: mysql_fetch_array(): supplied argument is not a valid MySQL Link to comment Share on other sites More sharing options...
justsomeguy Posted March 12, 2010 Share Posted March 12, 2010 I believe ORDER BY goes before LIMIT. You can print the error from MySQL if you want to see specifically what it doesn't like though. Link to comment Share on other sites More sharing options...
Greysoul Posted March 12, 2010 Author Share Posted March 12, 2010 doh didn't think of that. how do you call that alias name in my actual output code? $kpg isn't working of course. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 12, 2010 Share Posted March 12, 2010 $ps['kpg'] Link to comment Share on other sites More sharing options...
ShadowMage Posted March 12, 2010 Share Posted March 12, 2010 I believe ORDER BY goes before LIMIT. You can print the error from MySQL if you want to see specifically what it doesn't like though.Ooops... Wasn't sure about that. Good thing you're here jsg. Link to comment Share on other sites More sharing options...
Greysoul Posted March 12, 2010 Author Share Posted March 12, 2010 wow thats really cool, its almost like it images a temporary column lol. thanks for the help. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.