2016-03-11 61 views
-1

下面的函數是推車網頁上,在用戶已經添加的產品從以前的產品頁面。問題是我需要多維數組來更新購物車中的數量以添加相同的產品代碼。PHP會話更新的產品,如果ID已經在會議

有人能幫助我,如果語句,因此當同一產品代碼添加量的增加添加?

很喜歡這個答案我卻加入購物車是不同的。 PHP Sessions shopping cart: update product if it's already id the session

function AddToCart() 
{ 
$cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : ''; 
$itemcount = isset($_SESSION 

['itemcount']) ? $_SESSION['itemcount'] : 0; 
    { 
    $i = array_search($_POST['productcode'], $cart[PRODUCTCODE]); 
    $cart[PRODUCTCODE] [$itemcount] = $_POST['productcode']; 
    $cart[PRODUCTNAME] [$itemcount] = $_POST['productname']; 
    $cart[QUANTITY][$itemcount] = intval($_POST['quantity']); 
    $cart[PRICE][$itemcount] = $_POST['price']; 

$itemcount = $itemcount + 1; 
    if(strlen($error) == 0) { 
$_SESSION['cart'] = $cart; 
$_SESSION['itemcount'] = $itemcount; 
} 
    return $error; 

} 

回答

1

試試這個。

//check for an existing match 
$found = FALSE; 
if($cart){ 
    $idx = 0; 
    foreach($cart[PRODUCTCODE] as $idx => $product){ 
     if($product == $_POST['productcode']){ 
      $found = TRUE; 
      break; 
     } 
    } 
} 
//if we found a match 
if($found){ 
    $cart[QUANTITY][$idx] += intval($_POST['quantity']); 
} 
//otherwise add new item 
else{ 
    //your other code here 
} 
+0

什麼變量是IDX? – paul

+1

找到匹配的數組索引。這與您的itemcount相似。 – OffTheBricks

+0

香港專業教育學院增加了錯誤 注意:未定義不斷進行產品的使用 - 在 – paul