【问题标题】:Add up prices for total price with array values PHP用数组值 PHP 将总价格相加
【发布时间】:2016-11-03 16:14:38
【问题描述】:

我正在制作一个购物车,实际上它现在差不多完成了。我只想计算总价。问题是,每个产品的价格是在 foreach 循环中计算的(价格*数量),所以我不确定如何将所有价格相加。

PHP 函数:

    public function getCart(){
    $cartArray = array();
    if(!empty($_SESSION["cart"])){
        if($_SESSION['cart'] != ""){
            $cart = json_decode($_SESSION['cart'], true);
            for($i=0;$i<count($cart);$i++){
                $lines = $this->getProductData($cart[$i]["product"]);
                $line = new stdClass;
                $line->id = $cart[$i]["product"];
                $line->count = $cart[$i]["count"];
                $line->product = $lines->product;
                $line->total = ($lines->price*$cart[$i]["count"]);
                $cartArray[] = $line;
            }
        }
    }
    return $cartArray;
}

我如何全部显示:

<?php
    $cart = new cart();
    $products = $cart->getCart();

    $cartCount = 0;
    if(isset($_SESSION['cart'])){
        $cart = json_decode($_SESSION['cart'], true);
        $cartCount = count($cart);
    }
    if($cartCount > 0){
?>
<table class="table table-striped table-hover">
    <tr>
        <td align="left"><b>Product</b></td>
        <td align="center"><b>Quantity</b></td>
        <td align="center"><b>Total</b></td>
        <td align="right"></td>
    </tr>
    <?php
        foreach($products as $product){
    ?>
        <tr>
            <td align="left"><?php print $product->product; ?></td>

            <td align="center">
                <?php print $product->count; ?>
                &nbsp;&nbsp;
                <i style="cursor:pointer;" class="fa fa-minus lessQuantity" 
                data-id="<?php print $product->id; ?>"></i>
                <i style="cursor:pointer;" class="fa fa-plus addQuantity" 
                data-id="<?php print $product->id; ?>"></i>
            </td>

            <td align="center">$<?php print $product->total; ?></td>

            <td align="right">
                <span style="cursor:pointer;" data-toggle="tooltip" title="Delete item." 
                class="removeFromCart" data-id="<?php print $product->id; ?>"><i class="fa fa-trash"></i> Remove
                </span>
            </td>
        </tr>
    <?php 
        }
        } else {
            echo '<div class="alert alert-danger">No products in shopping cart!</div>';
        }
    ?>

    <tr>
        <td></td>
        <td></td>
        <td></td>
        <td align="right"><b>Total: $ Amount</b></td>
    </tr>
</table>

所以这个规则计算价格:

$line->total = ($lines->price*$cart[$i]["count"]);

但是我想将那条线的所有结果加起来为总价。有人可以帮我吗?

【问题讨论】:

  • 也许添加一个总计变量并在 foreach 循环中执行类似的操作:$grand_total += $line-&gt;total;
  • 顺便说一句,你怎么把会话数据保存为json?您可以在会话中保存数组($_SESSION 超级全局实际上是一个数组本身)。无需对购物车进行编码/解码...
  • 这是为了学校目的,我还在学习,这是我知道的!

标签: php html


【解决方案1】:

只需将产品总计添加到新变量即可。

循环购物车时:

<?php
    $amount = 0;
    foreach($products as $product){
        $amount += $product->total;
?>

循环之后:

<td align="right"><b>Total: <?= $amount ?></b></td>

【讨论】:

    【解决方案2】:

    您只需添加一个新变量,将之前的价格相加,然后将此变量添加到您的$cartArray

    public function getCart(){
        $cartArray = array();
        $cartArray["products"] = array();
        $totalCart = 0;
        if(!empty($_SESSION["cart"])){
            if($_SESSION['cart'] != ""){
                $cart = json_decode($_SESSION['cart'], true);
                for($i=0;$i<count($cart);$i++){
                    $lines = $this->getProductData($cart[$i]["product"]);
                    $line = new stdClass;
                    $line->id = $cart[$i]["product"];
                    $line->count = $cart[$i]["count"];
                    $line->product = $lines->product;
                    $line->total = ($lines->price*$cart[$i]["count"]);
                    $totalCart += $line->total;
                    $cartArray["products"][] = $line;
                }
            }
        }
        $cartArray["total"] = $totalCart;
        return $cartArray;
    }
    

    这将返回一个像这样的数组:

    Array(
       "products" => Array(
           [0] = ...
           [1] = ...
       ),
       "total" => 300
    );
    

    【讨论】:

      猜你喜欢
      • 2013-11-29
      • 1970-01-01
      • 2020-04-23
      • 2020-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多