有一个$_SESSION数组,它保存用户当前选择的产品(在购物车中)的ID。现在,当用户看到他的账单时,他想从他的购物车中删除一个项目(产品),我给他放了一个链接。但脚本不起作用。我在URL中配置了一个$_GET['itemid']
,通过使用它,我unset()
该数组元素。
但它不起作用。我该怎么办?这是我的代码
function remove_from_cart($stack_id) // stack_id is the id of the item in cart array ($_SESSION)
{
for($i=0; $i < count($_SESSION['add-to-cart-item']); $i++)
{
if($_SESSION['add-to-cart-item'][$i] == $stack_id)
{
unset($_SESSION['add-to-cart-item'][$stack_id]);
}
}
}
在您的代码中
if($_SESSION['add-to-cart-item'][$i] == $stack_id)
{
unset($_SESSION['add-to-cart-item'][$stack_id]);
}
您会发现$stack_id等于$_SESSION["地址到卡项目"][$i],而不是$i
这意味着您需要取消设置$_SESSION["地址到卡项目"][$i]。
祝好运
您应该取消设置[$i]
unset($_SESSION['add-to-cart-item'][$i]);