我设置了一个cookie。我想添加多个cookie,每个项目一个,并在每个值中都有这些项目的数量(这是购物车)。我的问题是,如何(例如)根据Cookie名称检查以查看有多少项。我不能只计算所有cookie,因为我也有将cookie用于其他目的。
您可以看到我如何设置它们-这可能会提供一些清晰度。我可以确认Cookie是否按预期设置。尝试按名称标识这些cookie时,我只是不知道从哪里开始。
if (!isset($_COOKIE['cart_item_' . $_GET['itemadd']])){
//if cookie doesn't, create one
setcookie($cookie_name, "1", time() + 86400, "/");
//echo "did i get here";
}else{
//if cookie does, add to quantity
if (!isset($_GET['quantity'])){
$cookie_value = $_COOKIE['cart_item_' . $_GET['itemadd']] + 1;
}else{
$cookie_value = $_GET['quantity']; //cookie value is the number of items being added
}
//echo "CN:" . $cookie_name . " - CV:" . $cookie_value;
setcookie($cookie_name, $cookie_value, time() + (86400), "/"); // 86400 = 1 day
答案 0 :(得分:1)
您可以将Session用于购物车。希望以下代码对您有所帮助:
if(!isset($_SESSION['some_product_id'])){
$data = array(
'product_name' => 'some product name',
'quantity' => 5, //some quantity,
'other_things' => 'Some other values',
);
$_SESSION['some_product_id'] = $data;
}else{
echo 'Product Name: '. $_SESSION['some_product_id']['product_name'] . '<br>';
echo 'Product Quantity: '. $_SESSION['some_product_id']['quantity'] . '<br>';
echo 'other_things: '. $_SESSION['some_product_id']['other_things'] . '<br>';
}