dqw70970 2014-03-08 12:07
浏览 34
已采纳

PHP数组,表,形式,乘法... argh

I have tried for hours, and hope you can help me now :

I am trying to get values from a HTML form via $_POST into a table via PHP, and multiplying two numbers: ['amount'] * ['price'] = $totalprice

So far so good.

I get something that might look like this:

Description – Amount – Format – Price – Total ($totalprice)

Spoon – 3 – pieces – 4 – 12

Plate – 2 – pieces – 3 – 6

Glass – 6 – pieces – 3 – 18

Now how do I sum up all the “Total” ($totalprice)’s 12+6+18 so I get the result: 36 ?

The piece of code I’m struggling with:

foreach($_POST['description'] as $value)
{
echo "<tr><td>";
echo $i+1;
echo "</td>
<td>".$value."</td>
<td><center>".$_POST['amount'][$i]."</td>
<td><center>".$_POST['format'][$i]."</td>
<td><center>".$_POST['price'][$i]."</td>";

//Figures out the total price = amount * price
$x1 = $_POST['amount'] [$i];
$x2 = $_POST['price'] [$i];
echo "<td><center>";
$totalprice = $x1 * $x2;
echo $totalprice;
//Figures out the total price = amount * price

$i++;
}
  • 写回答

2条回答 默认 最新

  • doumei1772 2014-03-08 12:10
    关注

    You just need to sum all totals while iterating

    $grand_total = 0;
    foreach($_POST['description'] as $value)
    {
        echo "<tr><td>";
        // code here...
    
        $totalprice = $x1 * $x2;
        echo $totalprice;
    
        $grand_total = $grand_total + $totalprice;
        $i++;
    }
    echo $grand_total;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?