2016-11-03 99 views
0

我正在購物車,實際上現在差不多完成了。我只想計算總價。問題是,每個產品的價格在foreach循環中計算(價格*數量),所以我不知道如何合計所有價格。用數組價值加總價格的價格PHP

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"]); 

但是,從該行所有的結果我希望被添加到總價格。有人可以幫助我嗎?

+0

也許添加總計變量,做這樣的事情在'foreach'循環:'$ grand_total + = $設備 - >總;' – Maximus2012

+0

順便說一句,你怎麼保存會話數據作爲JSON?你可以在會話中保存數組($ _SESSION超級全局實際上是一個數組本身)。無需編碼/解碼購物車... –

+0

這是爲了學校的目的,我還在學習,這是我知道的東西! – Jesse

回答

1

只需將產品總數添加到新變量即可。

當循環從購物車:

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

循環後:

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

你可以這樣做只是增加一個新的變量,其中和以前的價格,這個變量添加到您的$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 
);