gpt4 book ai didi

php - 相同键的特定 mysql_fetch_array 总和值

转载 作者:行者123 更新时间:2023-11-29 08:30:04 25 4
gpt4 key购买 nike

我的mysql结构如下:记录看起来像这样:

 id invoice_id position_name qty price_without_tax tax currency
1 12 Processor I7 2 120.00 23% €
2 12 Processor I5 3 80.00 15% €
3 12 Processor AMD 2 60.00 15% €
4 12 Processor I3 1 60.00 0% €

我必须通过 key 对税值进行求和才能得到类似的结果:

According to VAT rate | Net    | Vat Amount | Gross

23% | 240.00 | 55.20 | 295.20

15% | 360.00 | 54.00 | 414.00

0% | 60.00 | 0.00 | 60.00

TOTAL | 660.00 | 109.20 | 769.20

我想通过以下方式下载记录:

$position=mysql_query("SELECT * FROM position where invoice_id = '$id'");
while($row=mysql_fetch_array($position))
{
Sum there
}

但真的不知道如何计算增值税的总和。你能帮我吗?

最佳答案

首先,使用mysql_fetch_array不好。由于此扩展从 PHP 5.5.0 起已被弃用。更多信息: mysql_fetch_array

关于您的问题(使用您的代码)

$position=mysql_query("SELECT * FROM position where invoice_id = '$id'");

$products_by_vat = array();

while($row=mysql_fetch_array($position))
{
$row["tax"] = str_replace("%", "", $row["tax"]); // as far as I can see tax is a string not a number and has % at the end
if(!isset($products_by_vat[$row["tax"]])) {
$products_by_vat[$row["tax"]]["price"] = $products_by_vat[$row["tax"]]["vat_amount"] = 0.0;
}
$tmp_price = $row["qty"] * $row["price_without_tax"];
$products_by_vat[$row["tax"]]["price"] += $tmp_price;
$products_by_vat[$row["tax"]]["vat_amount"] += $tmp_price * ($row["tax"]/100);
}

echo "VAT % | PRICE | VAT AMOUNT | PRICE WITH VAT \n";

$total_price = $total_vat_amount = 0.0;
foreach($products_by_vat as $vat_percent => $vat_info) {
$total_price += $vat_info["price"];
$total_vat_amount += $vat_info["vat_amount"];

$price_with_vat = $vat_info["price"] + $vat_info["vat_amount"];
echo "{$vat_percent} | {$vat_info["price"]} | {$vat_info["vat_amount"]} | {$price_with_vat} \n";
}
$total_price_with_vat = $total_price + $total_vat_amount;
echo "TOTAL | {$total_price} | {$total_vat_amount} | {$total_price_with_vat} \n";

考虑到 Price_without_tax 是每 1 件产品的价格。

关于php - 相同键的特定 mysql_fetch_array 总和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16884216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com