2017-05-02 27 views
0

このphpトピックの新機能ですが、問題はセッション内のデータを追加する方法がわかりません。セッションデータとデータベースデータの結合

私は既に配列のパスを作成し、合計データを追加するためにいくつかの組み合わせを試みましたが、単価を乗算できませんでした。それらは常に個別に表示され、 )。

<div class="fresh-table"> 
    <?php 
    /* 
    * This is the console to get all the products in the database. 
    */ 
    $products = $con->query("select * from product"); 
    if(isset($_SESSION["cart"]) && !empty($_SESSION["cart"])): 
    ?> 
    <table id="fresh-table" class="table table-responsive"> 
     <thead> 
      <th data-field="cant" data-sortable="true">Quantity</th> 
      <th data-field="prod" data-sortable="true">Product</th> 
      <th data-field="total" data-sortable="true">Total</th> 
      <th data-field="actions"></th> 
     </thead> 
     <?php 
     /* 
     * From here we take the route of the products obtained and reflect them in a table. 
     */ 
     foreach($_SESSION["cart"] as $c): 
      $products = $con->query("select * from product where id=$c[product_id]"); 
      $r = $products->fetch_object(); 
     ?> 
     <tr> 
      <td><?php echo $c["q"];?></td> 
      <td><?php echo $r->name;?></td> 
      <td>$ <?php echo $c["q"]*$r->price; ?></td> 
      <td style="width:auto;"> 
       <?php 
       $found = false; 
       foreach ($_SESSION["cart"] as $c) { 
        if($c["product_id"]==$r->id){ 
         $found = true; 
         break; 
        } 
       } 
       ?> 
       <a rel="tooltip" title="Remove" class="table-action remove" href="cart/delfromfloat.php?id=<?php echo $c["product_id"];?> "> 
        <i class="fa fa-trash-o fa-lg"></i> 
       </a> 
      </td> 
     </tr> 
     <?php endforeach; ?> 
    </table> 
</div> 
</br> 
<span> 
    <h3> 
     <bold>Total: $ 
      <?php 
      foreach($_SESSION["cart"] as $pr): 
       $products = $con->query("select * from product where id=$pr[product_id]"); 
       $r = $products->fetch_object(); 
       $subtotal = $pr["q"]*$r->price; 
       $sumArr[] = $subtotal; 
       echo array_sum($sumArr); 
      ?> 
      <?php endforeach; ?> 
     </bold> 
    </h3> 
</span> 
</br></br> 
<a href="carrito.php" class="btn btn-danger"><i title="Go to cart" class="fa fa-cart"></i> Go to cart</a> 
<?php else:?> 
<p class="alert alert-warning">The cart is empty.</p> 
<?php endif;?> 
</div> 
+0

おそらくこれは数量倍の価格 '$ Cの[ "Q"] * $ R-です>あなたは 'echo $ subtotal = $ c [" q "] * $ r-> priceのような配列にそれを格納します。 $ sumArr [] = $ subtotal; 'ここで、総計したいところは、' echo array_sum($ sumArr); '...少なくとも、あなたの質問から私が理解していることです。私はカートが存在することをチェックしたり、アイテムをループしたりする以外に、セッションの部分がどこに来るのか分かりません。 – Rasclatt

+0

Hello @ Jhon117、 '$ _SESSION'を使う前に' session_start() 'セッションを開始したことを確認してください。 –

+0

がテストされましたが、結果は$ 15000 30000となり、15000が最初の製品、第2の製品は図示されず、2つの製品の合計を示す。 – Jhon117

答えて

0

私がコメントとして、あなたは、小計を格納し、総計でarray_sum()を使用する必要があります。

<?php 
function getCart($con) 
    { 
     if(empty($_SESSION["cart"])) 
      return array(); 
     # Store the ids for the sql query 
     # Store the qty values for later 
     foreach($_SESSION["cart"] as $c) { 
      $ids[] = $c['product_id']; 
      $qty[$c['product_id']] = $c['q']; 
     } 
     # Default array 
     $row = array(); 
     # Creating one query is more efficient then a whole bunch 
     $sql = "select * from product where id IN (".implode(', ',$ids).")"; 
     $con->query($sql); 
     # Fetch assoc so all values are arrays, not a mix of 
     # arrays and objects (for consistency and ease) 
     while($result = $con->fetch_assoc()) { 
      # Create the quantity value from stored 
      $result['qty']  = $qty[$result['id']]; 
      # Create the subtotal value from stored and db 
      $result['subtotal'] = $result['price'] * $qty[$result['id']]; 
      # Assign the row 
      $row[]    = $result; 
     } 
     # Return cart array 
     return $row; 
    } 
?> 
<div class="fresh-table"> 
    <?php 
    if(!empty($_SESSION["cart"])){ 
    ?> 
    <table id="fresh-table" class="table table-responsive"> 
     <thead> 
      <th data-field="cant" data-sortable="true">Quantity</th> 
      <th data-field="prod" data-sortable="true">Product</th> 
      <th data-field="total" data-sortable="true">Total</th> 
      <th data-field="actions"></th> 
     </thead> 
     <?php 
     $grandtotal = array(); 
     /* 
     * From here we take the route of the products obtained and reflect them in a table. 
     */ 
     foreach(getCart($con) as $item) { 
      $grandtotal[] = $c["subtotal"]; 
     ?> 
    <tr> 
     <td><?php echo $item["qty"] ?></td> 
     <td><?php echo $item['name'] ?></td> 
     <td>$ <?php echo $c["subtotal"] ?></td> 
     <td style="width:auto;"> 
      <?php 
      $found = false; 
      foreach ($_SESSION["cart"] as $c) { 
       if($c["product_id"]==$item['id']){ 
        $found = true; 
        break; 
       } 
      } 
      ?> 
      <a rel="tooltip" title="Remove" class="table-action remove" href="cart/delfromfloat.php?id=<?php echo $item["product_id"] ?> "> 
       <i class="fa fa-trash-o fa-lg"></i> 
      </a> 
     </td> 
    </tr> 
<?php } ?> 
</table> 
</div> 
</br> 
<!-- Just echo the stored subtotals here, don't loop again --> 
<span><h3><bold>Total: $<?php echo array_sum($grandtotal) ?></bold></h3></span> 
</br></br> 
    <a href="carrito.php" class="btn btn-danger"><i title="Go to cart" class="fa fa-cart"></i> Go to cart</a> 

<?php 
} 
else { ?> 
     <p class="alert alert-warning">The cart is empty.</p> 
<?php 
} ?> 

</div> 
関連する問題