开发者

php array cart problem?

I have the following code:

if($_SESSION['basket']){
    $i=0;
    while($i < count($_SESSION['basket'])) {    
        if(is_array($_SESSION['basket'][$i][$product['productid']]) && $_SESSION['basket'][$i][$product['productid']]['material']==$_POST['material'] && $_SESSION['basket'][$i][$product['productid']]['size']==$_POST['size']){
            $_SESSION['basket'][$i][$product['productid']]['qty']+=$_POST['qty'];
        }else{
            echo $i."-4";
            $_SESSION['basket'][][$product['productid']] = array("qty"=>$_POST['qty'], "material"=>$_POST['material'],"size"=>$_POST['size']);
        }
        $i++;
    }   
}else{
   开发者_如何学Python $_SESSION['basket'][][$product['productid']] = array("qty"=>$_POST['qty'], "material"=>$_POST['material'],"size"=>$_POST['size']);
}

and when I add an item to the basket the qty displayed for a product is 2 when I am only adding 1 also sometimes extra products are also sometime added can anyone help me?


I would double check that $_SESSION['basket'][$i][$product['productid']]['qty'] plus $_POST['qty'] equals your desired value for starters, echo it out, make sure they're ints (if you haven't already)

And try

$_SESSION['basket'][$i][$product['productid']]['qty'] = $_SESSION['basket'][$i][$product['productid']]['qty'] + $_POST['qty'];

instead of using the += operator. It's given me problems in the past.

Or your count($_SESSION['basket']) may be equaling 2, therefor running the addition twice.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜