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

时间:2015-05-15 05:50:44

标签: javascript php mysql

我正在使用预定义的php库存管理器。 所以在销售中我的数据以这种方式存储:

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

enter image description here

要在表I​​中显示这些值,请使用以下代码

$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;

我在这里得到了所需的输出,这是销售的总价值。

enter image description here

现在,我正在尝试提供一个报告功能,该功能将显示所有具有客户名称,销售价值的发票。 我想计算所有发票的总和并显示在表格行中,即$grand_total的总和。

我无法理解如何获得它。任何java脚本都可以做到吗?我不太了解js。所以我不知道它是否有可能。

提前致谢

1 个答案:

答案 0 :(得分:3)

因为您将数据存储在数据库中,所以您可以使用另一个查询来总结您要检索的所有记录中的所有grand_total。

grand_total

如果for(var i=1;i<=6;i++) { $("#span"+i).delay(i*1000).queue(function(){ $(this).html("Success"); }); } 是另一个查询的总结,则可以使用嵌套的select语句来执行此操作。

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

这个链接有一些关于嵌套选择的例子。