2016-12-14 110 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); 
} 
相关问题