duanmaduan1848 2011-11-27 18:03
浏览 95
已采纳

在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.

  • 写回答

2条回答 默认 最新

  • dongluo8303 2011-11-27 18:05
    关注
    $total = 0;
    while($row = mysql_fetch_array($qry))
    {  
      echo $row['b'] * $row['c'];
      $total += ($row['b'] * $row['c']);
    }
    
    // at the end, $total = grand total
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度