将商品添加到购物车时重复(php)

时间:2018-10-24 21:38:34

标签: php session model-view-controller backend

您需要在购物车中创建一个物品计数器。当我向购物车中添加项目时,它是重复的。在$ _SESSION中,['products']出现[0],其中也包含一个数字。我找到了摆脱它的方法:unset($ _ SESSION ['products'] [0]);但我不确定那是对的。我该怎么办?

此文件为order.php:

    <?php
class Order{
    static function addInOrder($id){
        $id= intval($id);
        $productsInOrder=array();
        if(isset($_SESSION['products'])){
            $productsInOrder=$_SESSION['products'];
        }

        if(array_key_exists($id, $productsInOrder)){
            $productsInOrder[$id]++;
        }

        else{
            $productsInOrder[$id]=1;
        }

        $_SESSION['products']=$productsInOrder;

    }

    static function orderCount(){
        if(isset($_SESSION['products'])){
            unset($_SESSION['products'][0]);
            $count=0;
            foreach ($_SESSION['products']as $id=>$value){
                $count+=$value;
            }
            return $count;
        }

        else{
            return 0;
        }
    }


}


?>

orderController.php:

    <?php
class OrderController{
    public function actionSend($id){
        Order::addInOrder($id);
        $referer=$_SERVER['HTTP_REFERER'];
        header("location: $referer");
    }
}
?>

1 个答案:

答案 0 :(得分:0)

您可能正在向addInOrder发送错误的信息,却看不到该变量是什么,根本就不可能知道它是什么。没有更多的代码,就无法知道两者的来源。即使您可以运行该网站,也很难追踪到这些东西(但下面有一些提示)。

例如,当您使用"foo"转换类似intval的字符串时,它将返回0。但是,如果id为falsy,则可以使用该函数,例如:

  //this is just a simplified example to show how to bail out of the method
 function addInOrder($id){
    if(false ==($id = intval($id))) return;
    echo $id."\n";
    //.....
 }

addInOrder(1);
addInOrder("foo");

输出(返回0)

 1

Sandbox

这确实不是解决方案,因为您应该发现发生错误的呼叫。

function addInOrder($id){
    if(false ==($id = intval($id))){
        try{
           throw new Excepton(__METHOD__." called with invalid argument[$id]");

        }catch(Exception $e){
             echo $e->getMessage()."\n";
             echo $e->getTraceAsString();
             exit;
        }
    }
    echo $id."\n";
 }

现在,当它发生时,您的站点将爆炸(只是在开玩笑),但会破裂,但是您会得到一个不错的堆栈跟踪,可用于查找调用的来源,从而可以解决实际问题。

基本上无论出于什么原因,您都会在方法中获得0变量的$id。最可能的原因(除了发送0之外)是其他原因,然后为其设置了一个数字,然后使用0将其转换为intval

  

在$ _SESSION中,['产品']出现[0]

例如,如果您调用addInOrder("foo"),则会得到一个数组项目,其中包含一个数字和一个索引为0

我不能在沙箱中使用$_SESSION,但是我们可以使用global $session复制它的工作方式。这样我就可以重现(我想你在说什么):

function addInOrder($id){
        global $session;
        $id= intval($id);
        $productsInOrder=array();
        if(isset($session['products'])){
            $productsInOrder=$session['products'];
        }

        if(array_key_exists($id, $productsInOrder)){
            $productsInOrder[$id]++;
        }else{
            $productsInOrder[$id]=1;
        }

        $session['products']=$productsInOrder;
}

addInOrder(1);
print_r($session);
echo "\n---------------------------------\n";
addInOrder("foo");
print_r($session);

输出:

Array
(
    [products] => Array
        (
            [1] => 1
        )

)

---------------------------------
Array
(
    [products] => Array
        (
            [1] => 1
            [0] => 1
        )

)

Sandbox

我个人认为,只要此方法在falsy之后出现intval之后,我都会抛出一个错误,只是因为除非调用弄乱(除非您不向其发送ID),否则它永远不会发生。但是我应该提到,永远不要在生产服务器上显示堆栈跟踪,因为参数可以包含DB密码等内容。

希望有帮助。