如何使用PHP删除数组会话中的现有值?

时间:2014-04-15 03:54:38

标签: php arrays session e-commerce cart

你能帮我解决一下我的代码吗?我正在创建一个超级简单的购物车,可以添加购物车/更新和删除。但就目前而言,我已经做的是添加到购物车和显示项目。但我有一个错误。每次刷新页面时,会话都会自动附加到会话数组。

现在我需要做的就是这个。

验证产品是否在会话列表中。如果没有为此创建会话,如果是,则跳过它。

还有其他问题。如何创建更新/删除功能?

到目前为止,这是我的代码。这个过程只有1个PHP文件。

$category   =   $_GET['cat'];
$product_id =   $_GET['product'];

//fn_print_r($_SESSION);
//unset($_SESSION['cart']);

$product_detail = get_allproduct_detail($product_id);

$prod_price = $product_detail['prod_price'];
$sale_price = $product_detail['sale_price'];
$prod_type  = $product_detail['prod_type'];

if(!empty($_POST['product_id']) && !empty($_POST['product_name']) && !empty($_POST['product_price']) && !empty($_POST['sale_price']) && !empty($_POST['qty'])) {

    $sess_id = $_POST['product_id'];
    $sess_name = $_POST['product_name'];
    $sess_price = $_POST['product_price'];
    $sess_sale_price = $_POST['sale_price'];
    $sess_qty = $_POST['qty'];

    $compute_total = $sess_sale_price * $sess_qty;

    $cart_row = array(
        'product_id' => $sess_id,
        'product_name' => $sess_name,
        'product_price' => $sess_price,
        'sale_price' => $sess_sale_price,
        'qty' => $sess_qty,
        'total' => $compute_total
    );

    if(!isset($_SESSION['cart'])){
        $_SESSION['cart'] = array();
    }

    $_SESSION['cart'][] = $cart_row;

    //fn_print_r($_SESSION);

} 

这是表格流程

<form method="POST" action="?page=product&cat=<?php echo $_GET['cat']; ?>&product=<?php echo $_GET['product']; ?>" onsubmit="">

    <div id="item_detail_right">

        <label>Qty:<input type="text" name="qty" value="1" size="5" style="text-align: center" />
        <input type="hidden" name="product_price" id="product_price" value="<?php echo $prod_price; ?>" />
        <input type="hidden" name="sale_price" id="sale_price" value="<?php echo $sale_price; ?>" />
        <input type="hidden" name="product_id" id="product_id" value="<?php echo $_GET['product']; ?>" />
        <input type="hidden" name="product_name" id="product_name" value="<?php echo strtoupper(get_product_name($_GET['product'])); ?>" />
        <input type="submit" value="+CART" />
        <input type="button" value="+Wishlist" id="mywishlist" data-wishlist-id="<?php echo $_GET['product']; ?>" />

    </div>

</form>

这是购物车的显示

if(!isset($_SESSION['cart']) || (count($_SESSION['cart']) == 0)) {
                echo "<p>Your cart is empty</p>";
            } else {

                echo "<table border='0' style='font-size: 12px; width: 100%' cellpadding='5'>";
                    echo "<tr>";
                        echo "<td style='background-color: white; color: black; text-align: center'>Product ID</td>";
                        echo "<td style='background-color: white; color: black; text-align: center'>Name</td>";
                        echo "<td style='background-color: white; color: black; text-align: center'>Price</td>";
                        echo "<td style='background-color: white; color: black; text-align: center'>Sale Price</td>";
                        echo "<td style='background-color: white; color: black; text-align: center'>Quantity</td>";
                        echo "<td style='background-color: white; color: black; text-align: center'>Total</td>";
                        echo "<td style='background-color: white; color: black; text-align: center'></td>";
                    echo "</tr>";

                    $total = 0;

                    foreach($_SESSION['cart'] as $item) {

                        echo "<tr>";
                            echo "<td style='text-align: center; background-color: gray; color: black'>".$item['product_id']."</td>";
                            echo "<td style='text-align: left; background-color: gray; color: black'>".$item['product_name']."</td>";
                            echo "<td style='text-align: right; background-color: gray; color: black'>".number_format($item['product_price'],2)."</td>";
                            echo "<td style='text-align: right; background-color: gray; color: black'>".number_format($item['sale_price'],2)."</td>";
                            echo "<td style='text-align: center; background-color: gray; color: black'><input type='text' name='cart_qty[]' value='".$item['qty']."' size='10' style='text-align: center'></td>";
                            echo "<td style='text-align: right; background-color: gray; color: black'>".number_format($item['total'],2)."</td>";
                            echo "<td style='text-align: center; background-color: gray; color: black'><a href='#'>Update this?</a> | <a href='#'>Remove this?</div></td>"; //how can I use this to remove and update the session?
                        echo "</tr>";

                        $total += ($item['sale_price'] * $item['qty']);

                    }

                    echo "<tr>";
                        echo "<td colspan='7' style='text-align: right'>";
                            echo "<label>Subtotal Amount: </label><input type='text' name='subtotal' value='".number_format($total,2)."' readonly='readonly'>";
                            echo "<input type='submit' value='Place Item' />";
                        echo "</td>";
                    echo "</tr>";

                echo "</table>";

            }   

这是我的数组

的示例输出
Array
(
    [visit] => nsrAROum86lb8VK
    [slideshow] => 04-15-14
    [cart] => Array
        (
            [0] => Array
                (
                    [product_id] => 1
                    [product_name] => AJNA
                    [product_price] => 90
                    [sale_price] => 81
                    [qty] => 1
                    [total] => 81
                )

            [1] => Array
                (
                    [product_id] => 1
                    [product_name] => AJNA
                    [product_price] => 90
                    [sale_price] => 81
                    [qty] => 1
                    [total] => 81
                )

            [2] => Array
                (
                    [product_id] => 1
                    [product_name] => AJNA
                    [product_price] => 90
                    [sale_price] => 81
                    [qty] => 1
                    [total] => 81
                )

            [3] => Array
                (
                    [product_id] => 1
                    [product_name] => AJNA
                    [product_price] => 90
                    [sale_price] => 81
                    [qty] => 1
                    [total] => 81
                )
           )
    )

我将整个代码上传到此链接

http://www.mediafire.com/view/g6jah2bxbzda04l/aroma_shop.php

1 个答案:

答案 0 :(得分:1)

您可以检查产品是否已在购物车中。

if(!isset($_SESSION['cart'])){
    $_SESSION['cart'] = array();
}

// Instead of appending $cart_row immediately,
// $_SESSION['cart'][] = $cart_row;

// only add the $cart_row which has not been added previously
$found = false;
foreach ($_SESSION['cart'] as $c) {
    if ($c['product_id'] == $cart_row['product_id']) {
        $found = true;
    }
}

if (!$found) {
    $_SESSION['cart'][] = $cart_row;
}