2017-12-30 56 views
1

제품이있는 다차원 배열이 있으며 새 항목을 추가하는 대신 사용자가 수량을 변경할 때 기존 항목을 업데이트하고 싶습니다. 병합을 시도하고 있지만 다차원 배열의 이름이 변경된 이유와 이유가 표시되지 않습니다.병합 대신 다차원 배열에 추가

그것은 그 다음과 같습니다 https://i.imgur.com/zEZVDXX.png

if(!empty($_POST["addquantity"])) { 
      $add_id = $_GET['add']; 
      $add_quantity = $_POST['addquantity']; 
      $sqlOrd = "SELECT * FROM products WHERE id_product = '$add_id'"; 
      $result = mysqli_query($conn, $sqlOrd); 
      while($row=mysqli_fetch_assoc($result)) { 
       $productByCode[] = $row; 
      } 

      $itemArray = array(
         $productByCode[0]["id_product"] => array(
          'name' => $productByCode[0]["name"], 
          'id_product' => $productByCode[0]["id_product"], 
          'quantity' => $_POST["addquantity"], 
          'price' => $productByCode[0]["price"] 
         ) 
        ); 

      if(!empty($_SESSION["cart_item"])) { 
       if(in_array($productByCode[0]["id_product"],$_SESSION["cart_item"])) { 
        foreach($_SESSION["cart_item"] as $k => $v) { 
          if($productByCode[0]["id_product"] == $k) 
           $_SESSION["cart_item"][$k]["quantity"] = $_POST["addquantity"]; 
        } 
       } else { 
        $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray); 
       } 
      } else { 
       $_SESSION["cart_item"] = $itemArray; 
      } 
     } 
+0

http://idownvotedbecau.se/imageofcode – theFunkyEngineer

+1

@theFunkyEngineer이 코드의 어떤 이미지입니다 - 바르 내용의 이미지가 약간의 설명을 포함,이 .. 당신이 불평 할 수있는 유일한 방법은, 그것은 그 인라인되지 않습니다. – Philipp

+1

코드를 해독해야합니다. 한 곳에서 너무 많이하고 있습니다. _highly_ SQL injection에 취약합니다. –

답변

0

그것은 문제가 in_array에 보인다하지만 당신은 array_key_exists을 사용하고 싶습니다. $_SESSION["cart_item"]에서 그러한 가치가 없기 때문에 문 ELSE if(in_array($productByCode[0]["id_product"],$_SESSION["cart_item"]))로 직접 이동이 경우

array_merge는 같은 정수 키 두 항목이 있기 때문에이 코드 $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);의 키를 재설정합니다.

그래서, 내 빠른 수정 제안 :

if(array_key_exists($productByCode[0]["id_product"],$_SESSION["cart_item"])) { 
... rest of the code ... 
+0

이제 작동 중입니다. 고맙습니다. 내가 in_array 정의를 잘못 읽은 것처럼 보입니다. –

1

당신은 전체 코드를 많이 단순화 할 수 있습니다. 루프는 필요 없으며 카트 항목의 키/ID를 알고 있으므로 직접 액세스 할 수 있습니다. 그 외에도 SQL 주입을 처리하고 준비된 문을 사용해야합니다.

if (!empty($_POST["addquantity"])) { 
    // cast some vars around to prevent sql injection - better use prepared statements 
    $add_id = (int)$_GET['add']; 
    $add_quantity = (float)$_POST['addquantity']; 
    $sqlOrd = "SELECT * FROM products WHERE id_product = '$add_id' LIMIT 1"; 
    $result = mysqli_query($conn, $sqlOrd); 
    $product = mysqli_fetch_assoc($result); 
    if (!$product) { 
     // some error handling.. 
    } 

    $item = [ 
     'name' => $product["name"], 
     'id_product' => $add_id, 
     'quantity' => $add_quantity, 
     'price' => $product["price"] 
    ]; 

    if (!isset($_SESSION["cart_item"])) { 
     $_SESSION["cart_item"] = []; 
    } 

    if (isset($_SESSION["cart_item"][$add_id])) { 
     $_SESSION["cart_item"][$add_id]["quantity"] += $add_quantity; 
    } else { 
     $_SESSION["cart_item"][$add_id] = $item; 
    } 
}