[SOLVED] Order an array
Posted: Thu Jul 08, 2004 7:25 pm
I am stuck, I have looked at the php.net manuals and cannot find anything to do with this:
My database is like so:
id | User | Hours
_____________
| 1 | Bob | 5
| 2 | Bob | 9
| 3 | Sam | 2
| 4 | Sam | 5
something like that, only alot more fields, and alot more users, but the basic idea is each row is the results of a form a user fills out, now my client decides he wants to be able to click any user and see the total
hours for that user, so I wrote the following code:
Now he decides not only be able to click a user to see the total hours, but have a list, for example top X users by hours, so he could define X as 5 it would show the top 5 users ordered most hours to less hours, this would have to be printed out in a table.
As for getting a list of all users I have a table in my database named "pilots" that holds users emails, etc...
so for a list of users i could simply do:
$sql = "SELECT * FROM pilots WHERE `hub` = '$hub' order by pid"
I would then have to calculate the hours for each user that I received from the pilots table (i could use the script i posted above, but im not sure EXACTLY how to approach this, arrays?
could anyone guide me how to do this or post some source code.. thanks
My database is like so:
id | User | Hours
_____________
| 1 | Bob | 5
| 2 | Bob | 9
| 3 | Sam | 2
| 4 | Sam | 5
something like that, only alot more fields, and alot more users, but the basic idea is each row is the results of a form a user fills out, now my client decides he wants to be able to click any user and see the total
hours for that user, so I wrote the following code:
Code: Select all
<?php
//Total hours
mysql_free_result($result);
// pid is username
$pid=$_REQUEST['pid'];
//pilotidnum is username
$sql = "SELECT totalhours FROM pirep where `pilotidnum` = '$pid'";
$result = mysql_query($sql) or die("Query failed : " . mysql_error());
$totalhours=0;
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
foreach ($line as $col_value) {
$totalhours=$totalhours + $col_value;
}
}
?>As for getting a list of all users I have a table in my database named "pilots" that holds users emails, etc...
so for a list of users i could simply do:
$sql = "SELECT * FROM pilots WHERE `hub` = '$hub' order by pid"
I would then have to calculate the hours for each user that I received from the pilots table (i could use the script i posted above, but im not sure EXACTLY how to approach this, arrays?
could anyone guide me how to do this or post some source code.. thanks