gpt4 book ai didi

php - 购物车总数量仅在更新后读取购物车中的最后一个数字

转载 作者:行者123 更新时间:2023-11-29 22:28:02 25 4
gpt4 key购买 nike

我正在尝试创建一个可以在购物车按钮中回显的总数量数字。截至目前,除非我单击更新,否则唯一要做的就是读取零。如果购物车中有 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;
?>

有人看出我做错了什么吗?

最佳答案

根据评论进行更改

foreach($_SESSION['shopping_cart'] AS $product['quantity']) {
$totalquantity = $totalquantity + $quantity;
}

foreach($_SESSION['shopping_cart'] AS $product) {
$totalquantity = $totalquantity + $product['quantity'];
}

关于php - 购物车总数量仅在更新后读取购物车中的最后一个数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30084581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com