我是饼干的新手是我如何设置和检索数据
if(!isset($_COOKIE['cart'])){
$_COOKIE['cart'] = array();
}
setcookie("cart[$stk_id]['name']", $name, time()+24*60*60, "/");
setcookie("cart[$stk_id]['quantity']", $qty, time()+24*60*60, "/");
setcookie("cart[$stk_id]['vendor']", $vendor, time()+24*60*60, "/");
foreach ($_COOKIE['cart'] as $stk_id => $product){
$qty = $product['quantity'];
$pro_name = $product['name'];
}
但我收到错误提示:未定义的索引:数量和名称。 有什么问题?
答案 0 :(得分:0)
Cookies数组存储通常由一次变量使用,你在数组中设置cookie名称,但是先创建数组,然后设置cookie名称更方便。在这里你可以设置json_encode
和json_decode
$data = array($stk_id =>
array(
"name" => $name,
"quantity" => $qty,
"vendor" => $vendor
)
);
setcookie("cart", json_encode($data), time()+24*60*60, "/");
$cookie = json_decode($_COOKIE['cart'],true);//for array output
foreach ($cookie as $stk_id => $product){
$qty = $product['quantity'];
$pro_name = $product['name'];
}