2016-12-14 26 views
0

今天,我正在爲用戶網站腳本開發購物車。當他們將商品添加到購物車時,它將存儲在名爲cartitems$_SESSION變量中。我將陣列存儲在$_SESSION['cartitems']的數組中,其中包含項目的itemid以及它們試圖添加到購物車的物品的數量。使用下面列出的代碼簡單地添加項目非常棒,但我需要他們增加數組中項目的值,假設他們嘗試添加更多相同的項目,而不是簡單地將新數組添加到SESSION中。這裏有一個例子:將項目添加到數組中,但是如果它們存在於數組中,則增加該值

-> User 1 visits the website. 
    - Add 5 melons to cart. 
    - Add 3 lemons to cart. 
    - Add 2 more melons to cart. 

我的陣列將打印出類似這樣:

array(
     array{0 => 1, 1 => 5}, 
     array{0 => 2, 1 => 3}, 
     array{0 => 1, 1 => 2} 
) 

..同時加入他們會是這樣,而不是像下面這樣的目標:

array(
     array{0 => 1, 1 => 7}, 
     array{0 => 2, 1 => 3} 
) 

使itemid爲1的值將增加到7.我還需要知道它在添加額外2之前的內容,因此庫存中只有6個瓜。不希望有人找到添加更多瓜的方法,那麼我們現在就留在股票領域!

我已經通過了股票領域的數量,隨着天氣它有無限的股票支持,或購買限制在一個項目,所以我有我需要的所有信息限制的東西(我已經做了添加項目時) ,只需要一種方法來改變數組,如果它已經在那裏增加數量就是全部。下面是我用它來添加項目代碼:

if(isset($_POST['quantity'])) { 
    // Cast quantity to an int for protection 
    $quantity = (int) $_POST['quantity']; 
    if(!empty($quantity) && $quantity > 0) { 
     $errors = 0; 
     // It doesn't support unlimited stock so we check stock level 
     if($unlimstock == "0") { 
      if($quantity > $stock) { 
       $quantity = $stock; 
      } 
      if($buylimit > 0) { 
       if($quantity > $buylimit) { 
        $errors = "1"; 
       } 
      } 
     } 
     if($errors == 0) { 
      $_SESSION['cartitems'][] = array($itemid, $quantity); 
      header("Location: cart.php"); 
      die(); 
     } 
    } 
} 

什麼是檢查它是否在數組中,如果是保值增值的最佳方法,如果不是像我已經我可以添加它,如果它是什麼價值,所以我知道它可以通過增加多少。多謝你們!

回答

1

爲了簡化代碼的$_SESSION['cartitems']應該將數據存儲:

$_SESSION['cartitems'] = [ 
    'product_id1' => 'quantity1', 
    'product_id2' => 'quantity2', 
]; 

然後更新量爲:

if (isset($_SESSION['cartitems'][$product_id])) { 
    $_SESSION['cartitems'][$product_id] += $quantity; 
} else { 
    $_SESSION['cartitems'][$product_id] = $quantity; 
} 

如果改變$_SESSION['cartitems']結構是不可能的,那麼你必須遍歷它:

$found = false; 
foreach ($_SESSION['cartitems'] as $key => $item) { 
    // I suppose that 0-indexed element stores id 
    if ($item[0] == $product_id) { 
     // I suppose that 1-indexed element stores quantity 
     $_SESSION['cartitems'][$key][1] += $quantity; 
     $found = true; 

     // break as certain element found 
     break; 
    } 
} 
if (!$found) { 
    $_SESSION['cartitems'][] = array($product_id, $quantity); 
} 
+0

非常好的建議!我會試試這個,謝謝。 – Kaboom

+0

如果遇到困難,我爲您當前的$ _SESSION添加了代碼。所以你可以比較哪一個更簡單) –

+0

第二個選項更好用,因爲它更容易用當前結構實現,而且我不一定知道會話中的所有項目,所以需要更多的寫作才能做出第一個選項工作,但第二個選項很好地工作。只需要添加我的支票現在是否超過股票限額。 – Kaboom

0

繼承人我做了什麼包括最後的事實chec k感謝@u_mulder:

// Set that we dont't see it by default 
$found = false; 
foreach($_SESSION['cartitems'] as $key => $item) { 
    if($item[0] == $itemid) { 
     // If it has unlimited stock, who cares, otherwise fact check it 
     if($unlimstock == "1") { 
      $_SESSION['cartitems'][$key][1] += $quantity; 
      $found = true; 
      break; 
     } else { 
      // If it's less than or equal to stock, we can try and add it 
      if(($_SESSION['cartitems'][$key][1] + $quantity) <= $stock) { 
       // If it has a buy limit, we set max to buy limit and check it 
       if($buylimit > 0) { 
        if(($_SESSION['cartitems'][$key][1] + $quantity) <= $buylimit) { 
         $_SESSION['cartitems'][$key][1] += $quantity; 
        } 
       } else { 
        $_SESSION['cartitems'][$key][1] += $quantity; 
       } 
      } 
      // Woot, we found it, so we can update it 
      $found = true; 
      break; 
     } 
    } 
} 
// If it wasn't found, we can add it as a new item. This has been fact checked already 
if(!$found) { 
    $_SESSION['cartitems'][] = array($itemid, $quantity); 
} 
相關問題