...

View Full Version : php table sum logic need



coolumanga
11-30-2010, 10:24 AM
hi again

i have a table with

Po no | Code | amount | SUM

Po-1 TT-2 5
Po-1 TT-2 10
Po-1 TT-2 15
Po-1 TT-2 10......... 40
Po-1 TT3 5
Po-1 TT3 5 ......... 10

how do i get the SUM of each code TT-2 amount total as 40 and TT-3 amount total as 10

plz give me a logic for with sample code

this above data come from database alsooooooooooo

plz provide sample code for this

mlseim
11-30-2010, 01:26 PM
This is an untested shot at your query ...

In this example, your table name is "tablename" ...
and the variable names as you provided (not sure if they are correct or not) ...



<?php

$query = "SELECT `Po no`, `Code`, SUM(`amount`)" .
"FROM tablename " .
"GROUP BY `Code`" .
"ORDER BY `Code` DESC";

$result = mysql_query($query);

// Print out result
while($row = mysql_fetch_array($result)){
echo "Total ". $row['Code']. " = ". $row['SUM(amount)'];
echo "<br />";
}

?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum