2011-09-06 12 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"& & $ 값이 == $는 true를 돌려줍니다 하위 ID 및 수량을 업데이트 할 수 있지만 그게 문제가 고객이 카메오를 구입하는 경우 같은 이드이지만 조각이나 조각에 다른 조각은 나와있을 것이다.

답변

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. 

}