2011-09-06 157 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. 

}