我目前正在大学学习PHP。我正在尝试创建一个函数来检查购物车会话中是否已经有商品。如果是,则将数量增加1,如果不是,则将项目添加到购物车阵列中。
添加新项似乎可以很好地工作,并且当添加同一项的倍数时函数也可以工作。然而,问题在于当我向数组添加另一项时。它会添加新项目,但如果我尝试放置另一个相同项目,它会将另一个项目添加到数组中,而不是将数量增加1。
for循环似乎不按我的意愿工作。如果有任何建议,我们将不胜感激,谢谢!
function addItemToCart($mysqli) {
$itemID = (int)$_GET['add'];
$counterItemsInCart = 0;
$alreadyInCart = false;
if(is_int($itemID)) { //makes sure the ID is passed as an int
$query = "SELECT product_id, product_name, product_price FROM product WHERE product_id = ?";
$stmt = $mysqli->prepare($query);
$stmt->bind_param("i", $itemID); // bind variables
$stmt->execute();
$result = $stmt->get_result();
if($rows = $result->fetch_assoc())
{
//setts all info needed to variables.
$pID = $rows['product_id'];
$pName = $rows['product_name'];
$pPrice = $rows['product_price'];
}
else
{
//if the item id is not an int display this message...
echo "not working";
}
if(isset($_SESSION['cart'])) { //check if any item in cart already
$intArraySize = count($_SESSION['cart']);
//loop through cart array
for($i = 0; $i < $intArraySize; $i+=1)
{
//check if item already exists in cart.
if($_SESSION['cart'][$i]["item_id"] === $pID)
{
$_SESSION['cart'][$i]["item_quantity"] += 1;
$alreadyInCart = true;
echo "<script type='text/javascript'>alert('item in cart');</script>";
}
}//loop to cycle through items in array...
if($alreadyInCart === false) { echo "<script type='text/javascript'>alert('item not in cart');</script>";
array_push($_SESSION['cart'], array($intArraySize =>array("item_id" => $pID, "item_name" => $pName, "item_price" => $pPrice, "item_quantity" => 1)));
}//close if statement check if same item is in array, if not add it.
}//close if statement checking if any item at all in shopping cart.
else {echo "<script type='text/javascript'>alert('new item');</script>";
$_SESSION['cart'] = array(array("item_id" => $pID, "item_name" => $pName, "item_price" => $pPrice, "item_quantity" => 1));
}
}//ends if statement checking if the id of the item is a int or string.
}//ends function
var_dump输出
array(3)>int(1)}[2]=>array(1){[2]=>array(4){["item_id"]=>int(2)["item_name"]=>字符串(14) "绿色w/Button"["item_price"]=>字符串(5)"14.99"["item_quantity"]=>int(1)}}}
这是一件接一件地增加了三件粉色衬衫,然后又增加了两件绿色衬衫。出于某种原因,绿色的被视为不同的物品。
很抱歉,如果格式不正确,不确定我应该如何格式化var_dump。现在将检查此项。。。
不应该是:吗
$_SESSION['cart'][$i][$pID]["item_quantity"] += 1;
好吧,我终于意识到我做错了什么。当我把另一个项目推到数组中时,请参阅以下内容:
array_push($_SESSION['cart'], array($intArraySize =>array("item_id" => $pID, "item_name" => $pName, "item_price" => $pPrice, "item_quantity" => 1)));
应该是:
array_push($_SESSION['cart'], array("item_id" => $pID, "item_name" => $pName, "item_price" => $pPrice, "item_quantity" => 1));
所以它为数组创建了另一个维度,这打乱了我的for循环。如果这有任何意义的话,正如我所说,我仍在学习php,不确定用什么术语来更好地解释它。
非常感谢大家的建议。使用马里乌斯建议的一些调试技巧帮助我发现了这个错误。
===
不应用于比较值。请尝试==
:
if($_SESSION['cart'][$i]["item_id"] == $pID)
{
$_SESSION['cart'][$i]["item_quantity"] += 1;
$alreadyInCart = true;
echo "<script type='text/javascript'>alert('item in cart');</script>";
}
http://www.w3schools.com/php/php_operators.asp
编辑:
如果不知道$_SESSION['cart']
是如何构建的,很难说还有什么问题