简体   繁体   English

PHP购物车,添加多个项目

[英]PHP Shopping cart, adding multiple items

if (!empty($_GET['action'])) {
    $j = $_GET['id_'];
    switch($_GET['action']) {
        case "add":
        $query = "select * from ogl where id = $j";
        $result = mysqli_query($conn, $query);
        $row = mysqli_fetch_assoc($result); 
        $array = array($row["id"]=>array('seller'=>$row["seller"], 
        'name'=>$row["name"], 'cpu'=>$row["CPU"], 'size'=>$row["size"], 
        'price'=>$row["price"], 'img'=>$row["img"])); 

        if (empty($_SESSION["cart_item"])) {
            $_SESSION["cart_item"] = $array;
        } else {
            $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"], $array);
        } 
        break;
    }
}

The problem is that it adds only one item by replacing the previously added one. 问题在于,它通过替换先前添加的项目仅添加了一项。 Each item has its ID which is sent by their button - GET method. 每个项目都有其ID,该ID通过其按钮GET方法发送。 What is the problem? 问题是什么? Thanks. 谢谢。

You are merging the contents of array into the existing array called "cart_item". 您正在将数组的内容合并到名为“ cart_item”的现有数组中。 However, any key of an array can only exist once. 但是,数组的任何键只能存在一次。 so by merging another shopping car item, you are overriding the one already there. 因此,通过合并其他购物车项目,您将覆盖已经存在的购物车项目。

You could handle it like this: 您可以这样处理:

    if (isset($_SESSION["cart_item"])) {
       // make sure the cart_item key exists before adding anything to it.
       $_SESSION['cart_item'] = array();
       $_SESSION["cart_item"][] = $array;
    } 
    else {
       $_SESSION["cart_item"][] =  $array;
    } 

However, this means that you can add the same product twice. 但是,这意味着您可以添加同一产品两次。 Since everything has their own ID, you could consider using $row['id] as the key for items. 由于所有内容都有自己的ID,因此您可以考虑使用$ row ['id]作为项目的键。

why dont you push an item into the array instead of replacing it ? 为什么不将项目推入数组而不是将其替换?

replace this line (and make the same for below code in the else) 替换此行(并在else中的以下代码中进行相同操作)

$_SESSION["cart_item"] = $array;

by 通过

$_SESSION["cart_item"][$j] = $array;

or better: 或更好:

$_SESSION['cart'][$j] = $array;

Make sure you define $_SESSION['cart'] somewhere near the start of the session 确保在会话开始附近的某个位置定义$ _SESSION ['cart']

make the cart stored in session be a string, with elements separated by a character like ';' 将存储在会话中的购物车设置为字符串,并用“;”等字符分隔元素 and those elements could themselves be lists separated by a different character like ','. 并且这些元素本身可以是由不同字符(如“,”)分隔的列表。

The session/cookie would look like: 会话/ cookie如下所示:

<id>,<seller>,<name>,<CPU>,<price>,<img>;
<id>,<seller>,<name>,<CPU>,<price>,<img>;
<id>,<seller>,<name>,<CPU>,<price>,<img>

This means that you could split the string with ";" 这意味着您可以使用“;”分割字符串。 and then every element in that array with ",". 然后该数组中的每个元素都带有“,”。

To implement this you would switch this: 要实现这一点,您可以切换:

$_SESSION["cart_item"] = array_merge($_SESSION["cart_item"], $array);

for this: 为了这:

$_SESSION["cart_item"] .= $row["seller"] . "," . $row["CPU"] . "," //and so on

Also, be carefull with sql_injection. 另外,请谨慎使用sql_injection。 You can change the "id_" parameter to a SQL query and damage you database. 您可以将“ id_”参数更改为SQL查询并损坏数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM