从PHP会话中删除值 [英] Removing values from PHP Session

查看:53
本文介绍了从PHP会话中删除值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个基本的购物车.购物车使用产品ID存储在会话中.

I am building a basic shopping cart. The cart is stored in a session uses product IDs.

我可以添加和删除项目.

I can add items and remove them.

如果一个项目被多次添加,则购物车正在对多个条目进行计数.

If an item is added more than once, the cart is counting the multiple entries.

我不确定如何更改这些数量.

I am not sure how to change these quantities.

展开购物车会话时,它看起来像这样:1,2,1,1

When exploding the cart session, it looks like this: 1,2,1,1

有3个乘积1和1个乘积1.

There is 3 x product 1 and 1 x product 1.

如果我删除产品1,它将删除所有正确的1个ID.

If I remove a product 1, it removes all the 1 IDs which is right.

但是我不确定如何删除其中的1个或设置其中应包含的数量.

But I'm not sure how to remove just 1 of them or set how many should be in there.

这是我的处理代码:

// Process actions
$cart = $_SESSION['cart'];
@$action = $_GET['action'];
switch ($action) {
case 'add':
    if ($cart) {
        $cart .= ','.$_GET['id'];
    } else {
        $cart = $_GET['id'];
    }
    break;
case 'delete':
    if ($cart) {
        $items = explode(',',$cart);
        $newcart = '';
        foreach ($items as $item) {
            if ($_GET['id'] != $item) {
                if ($newcart != '') {
                    $newcart .= ','.$item;
                } else {
                    $newcart = $item;
                }
            }
        }
        $cart = $newcart;
    }
    break;
$cart = $newcart;
break;
}
$_SESSION['cart'] = $cart;

有什么想法吗?

谢谢

Rob

推荐答案

您完全不应使用逗号分隔的字符串来存储购物车.相反,$_SESSION['cart']应该是包含产品数量的数组.

You should not be using a comma-delimited string to store your cart at all. Instead, $_SESSION['cart'] should be an array containing the quantities of products.

数组的结构变为$_SESSION['cart'][$product_id] = $quantity_in_cart

这使您可以从购物车中增加/减少数量.当它们达到0时,可以根据需要将其完全删除.与您当前正在尝试用逗号分隔的字符串进行修改相比,这实现起来要容易得多,而且要保持跟踪.

This allows you to increment/decrement quantities from the cart. When they reach 0, you can delete them entirely, if you wish. This is all far simpler to implement and keep track of than attempting to modify a comma-separated string as you are currently doing.

// Initialize the array
$_SESSION['cart'] = array();

// Add product id 1
// If the array key already exists, it is incremented, otherwise it is initialized to quantity 1
$_SESSION['cart'][1] = isset($_SESSION['cart'][1]) ? $_SESSION['cart'][1]++ : 1;
// Add another (now it has 2)
$_SESSION['cart'][1] = isset($_SESSION['cart'][1]) ? $_SESSION['cart'][1]++ : 1;
// Remove one of the product id 1s
$_SESSION['cart'][1]--;

// Add product id 3
$_SESSION['cart'][3] = isset($_SESSION['cart'][3]) ? $_SESSION['cart'][3]++ : 1;


// Delete the item if it reaches 0 (optional)
if ($_SESSION['cart'][1] === 0) {
   unset($_SESSION['cart'][1]);
}

然后免费获得一个查看物料数量的简便方法:

Then for free, you get an easy way to view item quantities:

// How many product 2's do I have?
$prod_id = 2;
echo isset($_SESSION['cart'][$prod_id]) ? $_SESSION['cart'][$prod_id] : "You have not added this product to your cart";

这篇关于从PHP会话中删除值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆