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>
這大概是數量乘以價格'$ C [ 「Q」] * $ R->價格「,所以你把它存儲在像'echo $ subtotal = $ c [」q「] * $ r-> price; $ sumArr [] = $ subtotal;'然後你想要總計,你只需'echo array_sum($ sumArr);'...至少這是我從你的問題中理解的。除了檢查購物車是否存在以及是否通過物品循環之外,我不知道會話部分在哪裏。 – Rasclatt
Hello @ Jhon117,在使用'$ _SESSION'之前,確保你已經開始會話頂部,即測試'session_start()' –
,但結果在屏幕上:總計:$ 15000 30000其中15000是第一個產品,第二種產品未顯示,並顯示了這兩種產品的總和。 – Jhon117