简体   繁体   中英

grand total sum in php

this may sound lame but i am stuck up with summing up the values retrieved from mysql. i want to sum up the grand total of the sales invoice items that is retrieved from mysql database. following is the code for sub total for each line which is fine.

while($row = mysql_fetch_array($qry)){  
echo $row['b'] * $row['c'];
}

'b' is the quantity and 'c' is the price.all these values are retrieved from mysql and the no of rows is variable and all this is inside the while loop. Now outside of the while loop, the last row, i want the grand total. can somebody point me in the right direction. i tried

echo SUM($row['b'] * $row['c']);

but this doesn't work and gives an error. i am sure i am doing it wrongly.

$total = 0;
while($row = mysql_fetch_array($qry))
{  
  echo $row['b'] * $row['c'];
  $total += ($row['b'] * $row['c']);
}

// at the end, $total = grand total

Why not use SQL for things SQL does best?

SELECT SUM(b*c)
FROM table

Simple as that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM