【发布时间】:2013-07-13 18:00:10
【问题描述】:
我有这段代码可以从购物车中删除任何想法...我需要删除两个$_SESSION。
1) $_SESSION["cart_array"]
2 $_SESSION["minicart"]
如果没有我添加$_SESSION["minicart"],它确实会删除$_SESSION["cart_array"],但是当我添加它时,我得到了minicart 部分,我得到了undefined index: minicart。所以我
试过
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) && !isset($_SESSION["minicart"]) || count($_SESSION[ “迷你车”])
上面的代码检查 // 如果购物车会话变量未设置或购物车数组为空*
到原始 if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"])
所以 我的问题
查看尝试过我在 如果这仍然不清楚,请发表评论,我会尽力再次解释。<?php
// if user wants to remove an item from cart
if (isset($_POST['index_to_remove']) && $_POST['index_to_remove'] != "") {
// Access the array and run code to remove that array index
$key_to_remove = $_POST['index_to_remove'];
if (count($_SESSION["cart_array"]["minicart"]) <= 1) {
unset($_SESSION["cart_array"]["minicart"]);
} else {
unset($_SESSION["cart_array"]["minicart"] ["$key_to_remove"]);
sort($_SESSION["cart_array"]["minicart"]);
}
}
?>
if 语句中做错了什么以及我在statement 中做错了什么以删除($_SESSION["cart_array"]) AND ($_SESSION["minicart"])
【问题讨论】:
-
只需在尝试取消设置之前测试密钥是否存在。
if (isset($_SESSION["cart_array"]["minicart"]) && count($_SESSION["cart_array"]["minicart"]) <= 1) -
@Orangepill 我仍然在第 73 行得到
undefined index:minicartif (count($_SESSION["cart_array"]["minicart"]) <= 1) {是 -
应该是
if (isset($_SESSION["cart_array"]["minicart"]) && count($_SESSION["cart_array"]["minicart"]) <= 1) -
那是你必须先问
isset()的地方。否则,您将使用 count 函数访问不存在的索引。 -
您最好使用
empty-函数。如果在空数组/字符串上使用count,它将返回undefined index错误。
标签: php session count session-variables unset