2011-09-06 154 views
0

我有一個功能,可以在購物車中添加自定義內置的影像。購物車存儲在會話變量中。如果客戶決定構建相同的客串,我不想在陣列中添加其他條目,我只希望能夠在該產品的數量上再增加1個條目。問題是當腳本達到檢查數組中存在的值是否返回false並將新條目添加到數組的位置時。我對PHP相當陌生,所以我不確定我是否正確地做了這件事。多維數組

function AddToCart($subpid,$subtype,$subprice,$subtotal,$subcarving,$subline1,$subline2){ 
    global $form; 

    $exist = false; 
    $i=0; 

    if(!isset($_SESSION['cart'])){ 
     $_SESSION['cart'] = array(0 => array("Product ID" => $subpid, "Type" => $subtype, "Price" => "$".$subprice, "Subtotal" => "$".$subtotal, "Carving" => $subcarving, "Line 1" => $subline1, "Line 2" => $subline2, "Quantity" => 1)); 
    } 
    else{ 
     foreach($_SESSION['cart'] as $item){ 
      $i++; 
      while(list($key,$value) = each($item)){ 
       /* If product exist add 1 to quantity */ 
       if($key == "Product ID" && $value == $subpid && $key == "Type" && $value == $subtype && $key == "Price" && $value == "$".$subprice && $key == "Subtotal" && $value == "$".$subtotal && $key == "Carving" && $value == $subcarving && $key == "Line 1" && $value == $subline1 && $key == "Line 2" && $value == $subline2){ 
        array_splice($_SESSION['cart'], $i-1, 1, array(array("Product ID" => $subpid, "Type" => $subtype, "Price" => "$".$subprice, "Subtotal" => "$".$subtotal, "Carving" => $subcarving, "Line 1" => $subline1, "Line 2" => $subline2, "Quantity" => $item['Quantity'] + 1))); 
        $exist = true; 
       } 
      } 
     } 
     if($exist == false){ 
      array_push($_SESSION['cart'], array("Product ID" => $subpid, "Type" => $subtype, "Price" => "$".$subprice, "Subtotal" => "$".$subtotal, "Carving" => $subcarving, "Line 1" => $subline1, "Line 2" => $subline2, "Quantity" => 1)); 
     } 
    } 
    return 0; 
} 

如果我只使用:$鍵==「產品ID」 & & $價值== $ SUBID它將返回true和更新的數量,但與問題是,如果客戶購買2個客串與相同的身份證,但雕刻不同或雕刻我的車會關閉。

回答

0

它不起作用,因爲您正在將每個密鑰與&&聲明同時進行比較,但是您一次只能循環使用一個密鑰。取出while循環,只是比較像這樣:

if($item['Product ID'] == $subpid ... //etc) { 

} 

而且你不需要array_splice剛剛更新的項目。

+0

感謝您的輸入與您的輸入我得到它的工作方式,我需要它 – Ray

1

我認爲你正在做這樣更加混亂比它必須是...

我會設置你的車陣像這樣:

$cart[0]["name"] = "whatever"; 
$cart[0]["ProductID"] = "1234"; 
$cart[0]["price"] = 0.00; 
$cart[0]["quantity"] = 1; 
$cart[0]["options"] = array(
    "subcarving" => "asdf", 
    "subline1" => "asdfsafd", 
    "subline2" => "asfdsadfdf"); 

然後,你可以只是處理它容易循環像這樣:

$didadd = 0; 
for($x = 0; $x < sizeof($cart); $x++) { 
    if($subid == $cart[$x]["ProductID"]) { 
     // check options 
     $sameOpts = 1; 
     foreach($cart[$x]["options"] as $key => $val) { 

      if($val != ${$key}) { // checks if the current items option[val] = function(val) 
       $sameOpts = 0; 
      } 

     } 

     if($sameOpts) { 
      $didadd = 1; // sets the flag that we added the element. 
      // increase quantity since the product id and options matched. 

     } else { 
      $didadd = 1; 
      // add new element 
      // sets the flag that we added the element 
     } 

    } 
} 

if(!$didadd) { 
    // still need to add the item 
    // do code to create new $cart item here. 

}