Forum Moderators: coopster

Message Too Old, No Replies

sorting variables

         

drollz

4:08 pm on Aug 30, 2003 (gmt 0)

10+ Year Member


I am a beginner in the truest sense. I have built a database of basketball statistics. I am only storing the most NECESSARY information. Example, I am not storing "points scored" because points scored = (field goals made * 2) + three-pointers made + free throws made.

After doing a select query to get all of the data from the table, I create a variable called $ppg for points per game [((field goals made * 2) + three-pointers made + free throws made) / games]. I use "( $row = mysql_fetch_array($result))" and "extract($row);" to put the data into variables. The problem is I can't figure out how to sort by a variable. In order to make my top 10 scorers list, I have to "cheat" by ordering by "((field goals made * 2) + three-pointers made + free throws made) / games" in my sql query. I want to be able to order the data by the variable $ppg. I have pasted my code below. I would greatly appreciate any help; keep in mind that I am a true beginner with experience only in MS Excel!

$getdata = "select * from ballers order by (((fgm * 2) + tpm + ftm) / g) desc";
$result = mysql_query($getdata)
or die ("COuldn't execute query");
$j = 0;
echo "<b><u>Top 10 Points Per Game</b></u><br>";
while ( $row = mysql_fetch_array($result))
{
extract($row);
$j++;
$ppg=(("$fgm" * 2) + "$ftm" + "$tpm" ) / "$g";
$f_ppg = sprintf("%01.1f",$ppg);
if ($j <= 10)
{
echo "$j. $firstname $lastname";
if ($team == "")
{
echo " *";
}
elseif ($team!= "")
{
echo " ($team)";
}
echo " $f_ppg<br>";
}
elseif ($j >= 11)
{
echo "";
}
}

justageek

5:16 pm on Aug 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You may want to move your echos out of the while. Just grab the db data in the while and store the values in an array, sort the array however you wish when you are done grabbing the data, then echo the results of the sorted array in a for loop.

RonPK

5:17 pm on Aug 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi drollz, welcome to WebmasterWorld!

It's generally best to let the database do as many calculations as possible, as it's usually faster than PHP. So letting the database order your results wasn't such a bad idea. I'd propose changing your query:

$getdata = "SELECT *, (((fgm * 2) + tpm + ftm) / g) AS ppg FROM ballers ORDER BY ppg desc";

Now you don't need to calculate the ppg again with PHP, as it's already in the query results.

In case you really need to do the sorting with PHP, you'll have to put the data into an array, and sort it with one of PHP's sort() functions.

drollz

5:36 pm on Aug 30, 2003 (gmt 0)

10+ Year Member



First I want to say THANK YOU to both of you guys. Like I said, I have no coding experience and I am trying to teach myself (with a broken hand no less) from a book ("PHP & MYSQL For Dummies") and the web, and it can be very frustrating. So I REALLY appreciate your help!

JUSTAGEEK: I will try your suggestion (if I can) and report back. Thanks.

RONPK: The problem is that I don't know how to do what you are suggesting. The data is in variables created by extract($row) (I think... Isn't it?) with the column names as variable (key?) names. How do I sort by $ppg for example and keep other columns such as $lastname and $firstname lined up?

For further clarification, here is what I'm trying to get-- and I am getting it... I just think I should be getting it another way.

Top 10 Points Per Game
1. Tracy McGrady (Orl) 32.1
2. Kobe Bryant (LAL) 30.0
3. Allen Iverson (Phi) 27.6
4. Shaquille O'Neal (LAL) 27.5
5. Paul Pierce (Bos) 25.9
6. Dirk Nowitzki (Dal) 25.1
7. Tim Duncan (SA) 23.3
8. Chris Webber (Sac) 23.0
9. Kevin Garnett (Min) 23.0
10. Ray Allen * 22.5

RONPK: Thank you for the tip on PHP vs. MYSQL speed. However, I'm still creating the $ppg variable anyway for display purposes. Furthermore, it just seems more concise to sort by the variable since if I wanted to change the formula for points scored, I would only have to change it in one place.

I know asort, arsort, etc., but I can't seem to get them to work.

If I use the AS statement in the SQL query, does that create a temporary column named ppg, which then creates a variable too?

justageek

7:08 pm on Aug 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Put the value you want to sort in the first element of the array and then do an array_multisort($yourarray,sort_desc). That should keep the keys aligned and give you the highest first.

IanKelley

9:19 am on Sep 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Just a side note... Giving MySQL too much to do can eat a lot of memory on your server. The few milliseconds of extra speed by having MySQL do all your work isn't always worth it.

Also, I've seen a heck of a lot of cases where PHP if faster for processing than MySQL.

ergophobe

1:34 am on Sep 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




The data is in variables created by extract($row) (I think... Isn't it?) with the column names as variable (key?) names. How do I sort by $ppg for example and keep other columns such as $lastname and $firstname lined up?

RonPK is abbreviating for brevity's sake. So you still need to SELECT for firstname, lastname but then you also SELECT for the formula he gives you which effectively creates a temporary column (ppg) to sort against, you sort DESC so that highest comes first, and $extract['ppg'] now gives you points per game with highest scorer first and you don't need to calculate it.

$i is your counter, from 1 to 10
echo $i . ". ". $extract['firstname'] . " " . $extract['lastname'] . " (" . $extract['team'] . " " . $extract['ppg'] . "\n";

should give you the desired output. If you have a small number of visitors, let it lie. If you are worried about server overhead, look at Ian's comment, do both and test.

Tom

drollz

4:21 am on Sep 5, 2003 (gmt 0)

10+ Year Member



Thanks a lot for all the help and advice guys.