Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php mysql购物车使用2d数组更新商品数量_Php_Mysql - Fatal编程技术网

php mysql购物车使用2d数组更新商品数量

php mysql购物车使用2d数组更新商品数量,php,mysql,Php,Mysql,我正在构建的购物车似乎只更新数组第一个元素的数量。例如,我的购物车中的第一个项目的数量为1,然后当我从产品页面添加另一个数量为2时,总数将更改为3,这就是我想要的。但是,如果我对另一个项目重复这些步骤,它将分别将它们添加到数组中,而不是将它们分组在一起 if(isset($_GET['add'])){ foreach ($_SESSION['cart'] as $key => $item){ if ($item['id'] == $itemID) {

我正在构建的购物车似乎只更新数组第一个元素的数量。例如,我的购物车中的第一个项目的数量为1,然后当我从产品页面添加另一个数量为2时,总数将更改为3,这就是我想要的。但是,如果我对另一个项目重复这些步骤,它将分别将它们添加到数组中,而不是将它们分组在一起

if(isset($_GET['add'])){
foreach ($_SESSION['cart'] as $key => $item){
            if ($item['id'] == $itemID) {

                $newQuan = $item['quantity'] + $quantity;

                unset($_SESSION['cart'][$key]);

                $_SESSION['cart'][] = array("id" => $itemID,"quantity" => $newQuan);
                header('Location:xxx');//stops user contsanlty adding on refresh
                exit;
            }
            else{
                $_SESSION['cart'][] = array("id" => $itemID,"quantity" => $quantity);
                header('xxx');//stops user contsanlty adding on refresh
                exit;
            }
        }
    }

有人能帮我解释一下为什么第一个元素只更新吗?

您的问题是foreach循环中的else情况。if检查第一个项目,然后-当第一个项目不匹配时-else案例激活并添加新项目

else{
            $_SESSION['cart'][] = array("id" => $itemID,"quantity" => $quantity);
            header('xxx');//stops user contsanlty adding on refresh
            exit;
        }
您想要做的是检查整个购物车,然后-如果没有找到文章-将其添加到购物车。为此,我建议使用一个变量来检查是否在循环中找到了条目。为了获得灵感,我在下面插入了代码。只需要进行一些小的更改:添加找到的变量并将其初始化(设置为not found),在if案例中将变量设置为found,并在退出foreach循环后检查变量是否已设置(如果未设置,则您肯定知道要将项目添加到购物车)


我没有对其进行测试,但这可以简单一点:

if(isset($_GET['add']))
{
    if(!isset($_SESSION['cart'])) $_SESSION['cart'] = array();
    if(!isset($_SESSION['cart'][$itemID]))
    {
        $_SESSION['cart'][] = array('id' => $itemID, 'quantity' => $quantity);
    }
    else
    {
        $_SESSION['cart'][$itemID]['quantity'] += $quantity;
    }
}

首先,问题和代码似乎还不够清楚,但我会尽力给出一些我认为可能有用的建议(我会做出一些假设)

这些变量来自哪里

$itemID, $quantity
假设他们是在
$\u GET
中出现的,我认为最好是这样保存您的购物车信息:

$itemCartIndex = strval($itemID);
//convert the integer item id to a string value -- or leave as string if already a string
$currentQuantity = (isset($_SESSION["cart"][$itemCartIndex]))? intval($_SESSION["cart"][$itemCartIndex]["quantity"]):0;
//set it by default if the index does not exist in the cart already
$currentQuantity += $quantity;
//update the quantity for this particular item
$_SESSION["cart"][$itemCartIndex] = array("quantity"=>$currentQuantity,...,"price"=>12.56);
//set up the index for this item -- this makes it easy to remove an item from the cart
//as easy as unset($_SESSION["cart"][$itemCartIndex]
完成后,将购物车显示/呈现给所有者是很简单的


祝你好运

谢谢你的帮助Gavin我最终从BetaminosSorry那里得到了答案我应该提到$itemID$数量都是get变量,我最终设法找到了解决方案,不过谢谢你的建议
$itemCartIndex = strval($itemID);
//convert the integer item id to a string value -- or leave as string if already a string
$currentQuantity = (isset($_SESSION["cart"][$itemCartIndex]))? intval($_SESSION["cart"][$itemCartIndex]["quantity"]):0;
//set it by default if the index does not exist in the cart already
$currentQuantity += $quantity;
//update the quantity for this particular item
$_SESSION["cart"][$itemCartIndex] = array("quantity"=>$currentQuantity,...,"price"=>12.56);
//set up the index for this item -- this makes it easy to remove an item from the cart
//as easy as unset($_SESSION["cart"][$itemCartIndex]