我正在尝试创建一个总数量数字,我可以回显到我的购物车按钮。截至目前,唯一的问题是读取零,除非我点击更新。如果购物车中有2个或更多不同的产品,我添加的总数量部分将是购物车中最后一个商品的两倍,而不会添加任何其他商品。
<?php
// Initialize cart
if(!isset($_SESSION['shopping_cart'])) {
$_SESSION['shopping_cart'] = array();
}
// Update Cart
if(isset($_POST['update_cart'])) {
$quantities = $_POST['quantity'];
foreach($quantities as $id => $quantity) {
if(!isset($products[$id])) {
$message = "Invalid product!";
break;
}
$_SESSION['shopping_cart'][$id]['quantity'] = $quantity;
}
if(!$message) {
$message = "Cart updated!<br />";
}
}
// Empty cart
if(isset($_GET['empty_cart'])) {
$_SESSION['shopping_cart'] = array();
}
if(empty($_SESSION['shopping_cart'])){
echo "Your cart is empty<br />";
}
else {
echo $message;
?>
<form action='./shoppingcart.php?view_cart=1' method='POST'>
<table class="carttable">
<tr>
<th class="cartth">Name</th>
<th class="cartth">Price</th>
<th class="cartth">Category</th>
<th class="cartth">Quantity</th>
</tr>
<?php
$base_price = 0;
foreach($_SESSION['shopping_cart'] as $id => $product) {
$product_id = $product['product_id'];
$base_price += $products[$product_id]['price'] * $product['quantity'];
$shipping_price += $products[$product_id]['shippingprice'] * $product['quantity'];
?>
<tr>
<td class="carttd"><?php echo "<a href='./viewProduct.php?view_product=$id'>" . $product['name'];?><?php echo $products[$product_id]['name']; ?> </a></td>
<td class="carttd"><?php echo '$' . $products[$product_id]['price']; ?></td>
<td class="carttd"><?php echo $products[$product_id]['category']; ?></td>
<td class="carttd">
<?php echo "<input type='text' name='quantity[$product_id]' value='" . $product['quantity'] . "' />"; ?> </td>
</tr>
<?php
}
//Calculates total
$total_price += $base_price + $shipping_price;
?>
<tr>
<td colspan="2"></td>
<td class="tdcarttotal">Subtotal</td>
<td class="tdcarttotal"><?php echo "$" . $base_price; ?> </td>
</tr>
<tr>
<td colspan="2"></td>
<td class="tdcarttotal">Tax</td>
<td class="tdcarttotal">$0</td>
</tr>
<tr>
<td colspan="2"></td>
<td class="tdcarttotal">Shipping Price</td>
<td class="tdcarttotal"><?php echo "$" . $shipping_price; ?></td>
</tr>
<tr>
<td colspan="2"></td>
<td class="tdcarttotal">Total</td>
<td class="tdcarttotal"><?php echo "$" . $total_price; ?></td>
</tr>
</table>
<input type='submit' id='button' name='update_cart' value='Update Cart'>
</form>
<form action="checkout.php?checkout=1">
<input type="submit" class="checkoutbutton" value="Proceed to Checkout">
</form><br><br><br><br><br>
<?php
}
//Shopping Cart Quantity Count
if(isset($_SESSION['shopping_cart']) && is_array($_SESSION['shopping_cart'])) {
$totalquantity = 0;
foreach($_SESSION['shopping_cart'] AS $product['quantity']) {
$totalquantity = $totalquantity + $quantity;
}
}
else {
$totalquantity = 0;
}
echo $totalquantity;
?>
有人看到我做错了吗?
答案 0 :(得分:0)
根据评论,更改
foreach($_SESSION['shopping_cart'] AS $product['quantity']) {
$totalquantity = $totalquantity + $quantity;
}
到
foreach($_SESSION['shopping_cart'] AS $product) {
$totalquantity = $totalquantity + $product['quantity'];
}