0
當我向購物車添加第二個項目,第一個項目將被取代
這是我的代碼(cart.php)如何將多個項目添加到購物車與cookies方法
<?php
include"connect.php";
?>
<?php
$total=0;
$variety = $quantity = $bran = "";
if(isset($_COOKIE['cart']))
{
$cookie = $_COOKIE['cart'];
$cookie = stripslashes($cookie);
$savedCardArray = json_decode($cookie, true);
foreach ($savedCardArray as $key => $value) {
$variety=$savedCardArray[$key][0];
$quantity=$savedCardArray[$key][1];
$bran=$savedCardArray[$key][2];
$total = $quantity*998;
setcookie('total', $total);
}
}
?>
的cart_update頁下面給出
<?php
class CartUpdate
{
/**
* Initialize the Cart.
* @return void
*/
public function __construct() {
if(isset($_COOKIE['cart'])) {
$cookie = $_COOKIE['cart'];
$cookie = stripslashes($cookie);
$savedCardArray = json_decode($cookie, true);
}
}
function add()
{
$variety = test_input($_POST["variety"]);
$rice_type = test_input($_POST["rice_type"]);
$quantity = test_input($_POST["quantity"]);
$bran = test_input($_POST["bran"]);
$items[]=array($variety,$quantity,$bran,$rice_type);
$json = json_encode($items);
setcookie('cart', $json);
}
我需要在這個餅乾方法的購物車中的所有選擇的項目。 任何人都可以幫助我?
你能否編輯上面的代碼? –
對不起,沒有編輯你的代碼,我沒有長時間使用PHP,也不想爲了讓語法正確而尋找東西:)我更新了我的答案,爲您提供了更多的見解。 –