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

标签 php html mysql

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

相关文章:

php - php无限循环会使MySQL服务器崩溃吗?

php - 在 PHP Core 中将现有字符串日期转换为 'm/j/Y'

PHP Soap Server如何知道被调用的方法

php - 多列关联数组的 CSS 格式化

javascript - Vuejs 使用 Props 访问模态组件中的数据

mysql - 如何在 MySQL 中将 utf8_general_ci 更改或转换为二进制?

php - 添加 doctype html 后,输入字段 CSS 代码无法正确呈现

html - 如何使 div 延伸到可滚动页面的底部? ASP

javascript - CSS3卡片翻转动画,不支持时检测

php - 添加 MYSQL 数组时遇到问题