有人可以教我如何为每种产品添加超过1个数量吗?
只需使用php session/array
。
<?php
session_start();
//unset($_SESSION['cart_items'])
// get the product id
$id = isset($_GET['id']) ? $_GET['id'] : "";
$name = isset($_GET['name']) ? $_GET['name'] : "";
$quantity = isset($_GET['quantity']) ? $_GET['quantity'] : "";
/*
* check if the 'cart' session array was created
* if it is NOT, create the 'cart' session array
*/
if(!isset($_SESSION['cart_items'])){
$_SESSION['cart_items'] = array();
}
// check if the item is in the array, if it is, do not add
if(array_key_exists($id, $_SESSION['cart_items'])){
// redirect to product list and tell the user it was added to cart
header('Location: atomyeveningcare4system.php?action=exists&id' . $id . '&name=' . $name);
}else{
// else, add the item to the array
$_SESSION['cart_items'][$name]=$name;
// redirect to product list and tell the user it was added to cart
header('Location: atomyeveningcare4system.php?action=added&id' . $id . '&name=' . $name);
}
?>
我认为问题来自以下代码:
// check if the item is in the array, if it is, do not add
if(array_key_exists($id, $_SESSION['cart_items'])){
// redirect to product list and tell the user it was added to cart
header('Location: atomyeveningcare4system.php?action=exists&id' . $id . '&name=' . $name);
}
请帮我解决这个问题。