PDA

View Full Version : [PHP] Is there any way to sort this easily?



Trigs
10-05-2010, 05:09 AM
$query = mysql_query("SELECT * FROM stats ORDER BY $getid DESC");

$getid can be either points, rebounds, or assists (already rows in the table). I also have another row called games. What I want to add is a way to calculate PPG, RPG, APG (points/rebounds/assists per game). The formula is points/reb/ast divided by the number of games. I don't want to store it in the database. Is there any easy way to do this?

Would it be possible to do:

mysql_query("SELECT * FROM stats ORDER BY '$getid / $games' DESC");

MattFr
10-05-2010, 02:56 PM
What are you on about?

Apolva
10-05-2010, 03:30 PM
I'm not 100% sure what you're asking, I'm assuming it's a leader-board?

I'm also not that great with SQL but I'll give it a shot..



$query=mysql_query("SELECT (`$getid`/`games`),`$getid`,* FROM `stats` ORDER BY (`$getid`/`games`) DESC");
while($row=@mysql_fetch_array($query)){
echo "<b>Games:</b> {$row['games']}<br /><b>".ucwords(strtolower($getid)).":</b> {$row[1]}<br /><b>Ratio:</b> {$row[0]}<hr />";
}
Note the funny looking back quotes: ``.

Then if you wanted username to be displayed as well, you'd use a JOIN.

LMS16
10-05-2010, 09:14 PM
<?php
$total = $getid/$games;
mysql_query("SELECT * FROM `stats` ORDER BY $total DESC");
?>


Something along those lines...

Lew.

Trigs
10-05-2010, 11:42 PM
It's a basketball stats script, to keep track of player's stats throughout the season, hence the points, rebounds, assists.

Thanks for that snippet, I'll try it out. I tried something similar to it earlier but it didn't work though.

MattFr
11-05-2010, 06:38 AM
The ORDER BY clause is used to order the results of the query and must, therefore, be a valid column in your table. So if the EXACT value of '$getid/$games' isn't a valid column reference, it isn't going to work.

I think the best move would be to stop overcomplicating things; simply lift the needed data from the table and use simple PHP calculations to work out what you need.

Want to hide these adverts? Register an account for free!