2014-12-19 107 views
1

我有會議存儲物品信息數組,每個物品都有我想要的數量,當用戶選擇相同的物品時只需增加數量而不是再次添加。 當我增加一個項目的數量沒關係,但是當我增加許多項目的數量只有它可以爲第一個,其他正在增加,但在同一時間他們複製數量1購物車會話重複數量

例如我選擇x 3次和y 2

X3 2 Y 1Y 1Y

//if it's the first item 
if (!isset ($_SESSION['Cart'])) 
    { 
$CartItem = array ($I_ID,$I_Name,$I_Price,$I_img,$quantity); 
$_SESSION['Cart'][] = $CartItem; 
header('Location:salad.php'); 
} 
//if the session contains items check if the item already exist 
else if (isset ($_SESSION['Cart'])) 
    { 
     foreach($_SESSION['Cart'] as $y) 
     { 
      if($y[1] == $I_Name) 
       { 
        $_SESSION['Cart'][$i][4]++; //increment quantity 
        header('Location:salad.php'); 
        break; 
       } 

      else{ 
       $CartItem = array($I_ID,$I_Name,$I_Price,$I_img,$quantity); 
       $_SESSION['Cart'][] = $CartItem; 
       header('Location:salad.php'); 
       } 
       $i++; 
     } 
    } 

>

回答

1

的效應初探是這裏:

{

foreach($_SESSION['Cart'] as $y) 
    { 
     if($y[1] == $I_Name) 
      { 
       $_SESSION['Cart'][$i][4]++; //increment quantity 
       header('Location:salad.php'); 
       break; 
      } 

     else{ 
      $CartItem = array($I_ID,$I_Name,$I_Price,$I_img,$quantity); 
      $_SESSION['Cart'][] = $CartItem; 
      header('Location:salad.php'); 
      } 
      $i++; 
    } 
} 

如果第一項是不一樣的操作系統$ i_NAME您添加了爲什麼你有很多項目的項目。

我會做這樣的事情:

{ 

     $addItem = true; 
     foreach($_SESSION['Cart'] as $y) 
     { 
      if($y[1] == $I_Name) 
       { 
        $_SESSION['Cart'][$i][4]++; //increment quantity 
        $addItem = false; 
        header('Location:salad.php'); 

        break; 
       }      
       $i++; 
     } 
     if($addItem){ 

       $CartItem = array($I_ID,$I_Name,$I_Price,$I_img,$quantity); 
       $_SESSION['Cart'][] = $CartItem; 
       header('Location:salad.php'); 
       } 
     } 
    } 

你參考更好渲染它& $ y和唐噸使用$我++;

+0

這是工作(f)@mouradk – huda 2014-12-19 11:42:02