dongshushen4392 2015-05-15 05:50
浏览 30
已采纳

PHP - 获取表中显示的值的总和

I am using a pre defined php inventory manager. So in sales my data is getting stored this way:

[{"product_id":"8","total_number":"70","selling_price":"110"}]

enter image description here

To display these values in table I use the following code

$sub_total          =   0;
$invoice_entries    =   json_decode($row['invoice_entries']);
foreach ($invoice_entries as $row2):
    $sub_total  +=  ($row2->total_number * $row2->selling_price);                                       
endforeach;

$sub_total      =   $sub_total - ( $sub_total * ($row['discount_percentage'] / 100) );
$grand_total    =   $sub_total + ( $sub_total * ($row['vat_percentage'] / 100) );
echo $grand_total;

I get the desired output here which is the total value of the sale.

enter image description here

Now, I am trying to give a report feature which will show all the invoices with the client name, sale value. I want to calculate the total of all the invoices and show in a table row i.e. Total of $grand_total.

I am unable to understand how do I get that. Any java script could do it? I don't understand js well. So I have no clue if its possible with it or not.

Thanks in advance

  • 写回答

1条回答 默认 最新

  • dongliu6848 2015-05-15 06:55
    关注

    since you storing data in database, you can use another query to sum up all the grand_total from all the record you want to retrieve.

    select sum(grand_total) from yourTable where yourFilter

    if grand_total is sum up from another query, you can use nested select statement to do it.

    http://www.mysqltutorial.org/mysql-subquery/

    this link have some example on nested select.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历