0

我在将产品添加到全局数组时遇到了一个小问题。这可以在购物车中使用。这是我们关注的代码部分:

if ( isset($_POST['id']) ){ // If the product is adding to cart. This product Id is sended via form.
            $productid = mysql_real_escape_string($_POST['id']); 
            $cartproduct = mysql_query("select * from stuff where id = '$productid'");
            $Addrow=mysql_fetch_assoc($cartproduct);
            if ($Addrow['qty']<$_POST['qty']){      // the product quantity that will add to cart can't be greater than in database
                $_POST['qty']=$Addrow['qty'];
            }

                $new_product = array(array('name'=>$Addrow['name'], 'id'=>$Addrow['id'], 'price'=>$Addrow['price'], 'qty'=>$_POST['qty'])); // Creating new product info in array
                if (isset($_SESSION['cart'])){ // If the cart exist
                    foreach ($_SESSION['cart'] as $add_product){
                        if ($add_product['id']==$_POST['id']){ // checking that product is already in $_SESSION
                            $exist = TRUE; 
                        }else{
                            $exist = FALSE;
                        }
                    }
                    if ($exist == TRUE){ // If The product is in the $_SESSION: Update amount
                        // I dont have code for it.     
                    }else{ // The product is not in array, add it.
                        $_SESSION["cart"] = array_merge($_SESSION["cart"], $new_product);
                    }
                }else{ // If the cart is not exist
                    $_SESSION['cart']=$new_product;
                }
        }

问题是当我尝试添加已经在数组中的产品时。该功能正在将其添加为新产品...

第二个问题是删除这些产品。我不能这样做:

    foreach ($_SESSION['cart'] as $remove){
            if($_GET["id"] == $remove['id']){
                unset($_SESSION["cart"][$remove]);              
            }
        }

任何人都可以帮助解决它?

4

1 回答 1

0

我建议稍微改变一下数组。在“购物车”中,使用产品 ID 作为产品的键。这样,您可以轻松地在阵列中查找和更新产品。

您可以在会话中更改购物车数组。因为键在数组中是唯一的,所以为键设置一个值会覆盖前一个值。

因此,我添加了您内部代码的略微修改版本。它执行三个步骤:

  1. 将 post 变量添加到普通变量。我发现这更容易使用,您可以在继续之前进行各种其他检查(例如检查数量是否 > 0 等)。

  2. 从数组中获取现有产品或初始化新产品。这使用array_key_exists,因为我认为这是最纯粹的检查,但人们也使用
    isset($_SESSION['cart'][$productId]),这也应该有效。无论如何,这样的检查比使用循环更好(更快,更容易),但只有当您切换到使用产品 ID 作为键时,它才会起作用。

  3. 只需设置或更新数量并将更新的产品写回数组。如果产品之前存在,它只会覆盖之前的值。

代码变为:

// Use a variable. It's easier and more readable.
$productId = $_POST['id'];
$quantity = $_POST['qty'];

// Your other checks go here. Left out for brevity.

// Get the current product from the cart, if it exists.
// If not, create a new product.
if (array_key_exists($productId, $_SESSION['cart'])) {
  // Product found, get it and update its quantity.
  $product = $_SESSION['cart'][$productId];
  $product['qty'] += $quantity;
} else {
  // Product not found. Initialize a new one.
  $product = array(
        'name' => $Addrow['name'], 
        'id' => $Addrow['id'], 
        'price' => $Addrow['price'],
        'qty' => $quantity);
}

// Write updated or new product back to the array, and use the product id as key.
$_SESSION['cart'][$productId] = $product;

其他一些提示:

  • mysql_*如果您有机会切换到 mysqli 或 PDO,请不要使用这些功能。这些mysql功能已弃用。
  • 确保检查查询结果。可能出了点问题(或者有人伪造了请求),并且在数据库中找不到产品 ID。在这种情况下,$Addrow可能是falsenull。确保检查这一点并显示适当的错误,而不是更新购物车,这可能会损坏您的购物车。
  • 如果无法添加数量,我不会默默降低数量,因为用户会认为他们发现了一个错误。相反,明确说明这样的数量是不可用的。
  • 您可能需要重新考虑这一点。毕竟,也许今天会交付其他库存,或者其他人可能会同时订购最后一件商品。所以最好稍后检查,当订单保存完好并且您想要处理它时。
  • 在购物车中显示有关可用数量的信息将使您的竞争对手了解您拥有的库存数量,他们也可以从中推断出其他信息。此外,他们甚至可能会下假订单以使产品在您的网站上不可用。这是一个狗咬狗的世界。小心你显示的信息。
于 2015-02-17T17:00:13.593 回答