2016-11-03 19 views
0

私はショッピングカートを作っていますが、実際にはほぼ完成しています。私は総価格を計算したいだけです。問題は、foreachループで製品ごとの価格が計算される(価格*数量)ので、すべての価格をどのように追加するかわからないということです。したがって、このルールは、価格を計算配列の値で合計金額を値段にするPHP

<?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> 

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

$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 
); 
関連する問題