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