【问题标题】:calculating multiple shipping orders (php / mysql)计算多个运输订单(php / mysql)
【发布时间】:2013-10-04 11:30:25
【问题描述】:

需要帮助解决一些问题。

我正在建立一个运输网站,这里客户可以下多个订单(最多 10 个)我使用 PHP 和 MySQL,必须将每个订单插入到表中,每个订单都有一个唯一的 ID,所以每个订单都输入换行。

代码:

//...

if(isset($_POST["order_price$i"]))
    $order_price=htmlspecialchars($_POST["order_price$i"]);

if(isset($_POST["order_number$i"]))
    $order_number=number_format(htmlspecialchars($_POST["order_number$i"]));

//...

它可以成功添加到数据库中,我的问题是:我希望能够计算订单总额并添加到客户的订单详细信息中。
我这样做了,但它不起作用:

$total_cost = $order_price*$order_number;

请问我做错了什么?

【问题讨论】:

    标签: php mysql shipping


    【解决方案1】:

    将 order_price 转换为数字格式

    【讨论】:

      【解决方案2】:

      您应该将总成本添加到存储在总成本中的先前值(并希望这是一个循环)

         $total = $order_price*$order_number;
         $total_cost = $total_cost + $total;
      

      【讨论】:

        【解决方案3】:

        您的代码运行良好,请参见此处

        <?php
        
        $i = 2;
        $_POST["order_price1"] = 20;
        $_POST["order_number1"] =1;
        $_POST["order_price2"] = 10;
        $_POST["order_number2"] =3;
        $total_cost = 0;
        for($count = 1; $count <= $i; $count ++){
        
            if(isset($_POST["order_price$count"]))$order_price=htmlspecialchars($_POST["order_price$i"]);
        
            if(isset($_POST["order_number$count"]))$order_number=number_format(htmlspecialchars($_POST["order_number$i"]));
        
            $total_cost =$total_cost + ($order_price * $order_number);
        
        }
        
        
        echo $total_cost;
        
        ?>
        

        我在这里测试过:

        http://ideone.com/niNxqZ
        

        也许这是您的意见。尝试记录您的输入和变量,这样您就可以跟踪正在发生的事情。情况是,您的输入有特殊字符,或者它为空。或者您将 total_cost 放在循环之外。基本上你的代码可以工作,它在迭代中。

        【讨论】:

        • 它有效,是的,我知道但不是我想要的,假设客户想要下达 5 个不同的订单,$total_cost 只会计算最后一个订单。我想要 $total cost 来计算所有价格 * 所有数字。从表格
        • 更新了代码,基本数学:$total_cost =$total_cost + ($order_price * $order_number);
        • 它有帮助,我能够纠正这个问题。谢谢。
        • 您好,您的问题已经得到解答,我很乐意通过聊天回答您的其他问题。您能否选择一个答案并关闭此问题。因为我们已经跑题了。
        猜你喜欢
        • 2012-01-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-25
        • 1970-01-01
        • 2017-05-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多