计算总价格php

时间:2014-01-24 11:25:59

标签: php arrays foreach sum

现在我正在运行这段代码:

$count = 0;
    foreach ($products as $key => $product){
      $number = isset($_POST[$key])?$_POST[$key]:'';
            if (!$number){
            $count++;
            echo "";
            } else {
        echo "<tr>";
            echo "<td>";
                echo $product;
            echo "</td>";
            echo "<td>";
                echo $number;
            echo "</td>";
            echo "<td>";
                $price = $prices[$count]* $number;
                echo "&#8364 ", $price;
            echo "</td>";
        echo "</tr>";

        $count++;
        }
    }

但是现在我添加了第3行(我的第一行只是<th>),看起来像这样:

echo "<tr>";
    echo "<td>";
        echo "Totaal";
    echo "</td>";
    echo "<td>";
        echo $totalproducts;
    echo "</td>";
    echo "<td>";
        echo $totalprice;
    echo "</td>";
echo "</tr>";

正如你所看到的那样,我总共花了2美元...在这一行中你可以猜到需要订购的产品总数和这些产品的总价。

我的问题是我真的不知道如何让它总结所有的$ price并将其放在$ totalprice中并总结所有$ number并将其放入$ totalnumber中。我知道这是一个基本的问题,但是很久以前,因为我实际上使用PHP。我坚持这个。

感谢您对此事的任何帮助,提前谢谢!

2 个答案:

答案 0 :(得分:1)

试试这个......

$totalprice=0;
$count = 0;
    foreach ($products as $key => $product){
      $number = isset($_POST[$key])?$_POST[$key]:'';
            if (!$number){
            $count++;
            echo "";
            } else {
        echo "<tr>";
            echo "<td>";
                echo $product;
            echo "</td>";
            echo "<td>";
                echo $number;
            echo "</td>";
            echo "<td>";
                $price = $prices[$count]* $number;
                $totalprice = $totalprice+ $price;
                echo "&#8364 ", $price;
            echo "</td>";
        echo "</tr>";

        $count++;
        }
    }

echo "<tr>";
    echo "<td>";
        echo "Totaal";
    echo "</td>";
    echo "<td>";
        echo $total_products;
    echo "</td>";
    echo "<td>";
        echo $totalprice;
    echo "</td>";
echo "</tr>";

答案 1 :(得分:0)

我希望你想要这个

首先设置变量$total_products$total_price

$total_products = 0; // set to 0 initially
$total_price = 0; // set to 0 initially

    $count = 0;
        foreach ($products as $key => $product){
          $number = isset($_POST[$key])?$_POST[$key]:'';
                if (!$number){
                $count++;
                echo "";
                } else {
            echo "<tr>";
                echo "<td>";
                    echo $product;
                echo "</td>";
                echo "<td>";
                    echo $number;
                echo "</td>";
                echo "<td>";
                    $price = $prices[$count]* $number;
                    echo "&#8364 ", $price;
                echo "</td>";
            echo "</tr>";

            $total_products += 1; // update total_products +1
            $total_price += $price;  // update total_price +$price
            $count++;
            }
        }

并将它们放在这里

echo "<tr>";
    echo "<td>";
        echo "Totaal";
    echo "</td>";
    echo "<td>";
        echo $total_products;
    echo "</td>";
    echo "<td>";
        echo $total_price;
    echo "</td>";
echo "</tr>";