Jump to content

SQL SUM with array?


mattichu

Recommended Posts

Hi!

 

i have a table:

 

 

 

Day        Username          Rate      Hours        Shift Cost

--------------------------------------------------------------------

MON          Matt                  8.5            2                £17

MON        Imogen                6              2                £12

MON          Matt                  8.5            1                £8.5

MON        Imogen              6              1                  £6

--------------------------------------------------------------------

 

 

 

Im trying to output a table that shows the total cost for each user.

 

like:

 

 

User        Rate    Hours    Wage

----------------------------------------

Matt        8.5        3        £25.5

Imogen    6          3        £18

----------------------------------------

 

 

How would i go about doing this??

 

many thanks!

 

x

Link to comment
https://forums.phpfreaks.com/topic/258344-sql-sum-with-array/
Share on other sites

SOLVED

 

<?


$username="****";
$password="*****";
$database="******";
mysql_connect('localhost',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="select username,rop, sum(totalcost) as totalcost, sum(totalhours) as totalhours from hours group by username";	 
$result = mysql_query($query) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($result)){

?>
<html>
<table><td><?php echo $row['username']; ?></td><td><?php echo $row['rop'] ; ?></td><td><?php echo $row['totalhours'] ; ?></td><td><?php echo $row['totalcost'] ; ?></td>
</table>
</html>
<?	
}
?>

 

many thanks guys!

Link to comment
https://forums.phpfreaks.com/topic/258344-sql-sum-with-array/#findComment-1324284
Share on other sites

That's the general idea, some suggestions:

- Use complete tags (<?php).

- Better formatting.  It will help you in the long run.

- Instead of using or die (which I think is OK for debugging) properly handle errors - http://www.phpfreaks.com/blog/or-die-must-die

 

Link to comment
https://forums.phpfreaks.com/topic/258344-sql-sum-with-array/#findComment-1324288
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.