dr200166 2014-01-24 03:25
浏览 127
已采纳

计算总价格php

Right now i have this piece of code running:

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

But now i added a 3rd row (my first row is just the <th>'s) beneath that one looking like this:

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

As you can see i placed 2 $total... in this row where as you can guess need to come the total products ordered and total price of those products.

My problem is that i don't really know how to let it sum up all the $price and place this in $totalprice and sum up all $number and place it inside the $totalnumber. I know it's a basic question but it has been a long time ago since i actually worked with php. And i'm stuck on this.

I appreciate any help on this matter, thanks in advance!

展开全部

  • 写回答

2条回答 默认 最新

  • duanhuo7441 2014-01-24 03:32
    关注

    i hope you want this

    first set the variable $total_products and $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++;
                }
            }
    

    and put them here like

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

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部