douhuan7862 2018-10-24 21:38
浏览 225
已采纳

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

You need to create an item counter in the shopping cart. When I add an item to the cart, it is duplicated. In $_SESSION ['products'] appears [0], which also contains a number. I found a way to get rid of it: unset($_SESSION['products'][0]); but I'm not sure that's right. What should I do?

this file is 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条回答 默认 最新

  • doubi3929 2018-10-24 22:07
    关注

    You are probably sending the wrong information to addInOrder without seeing what that variable is there is no way what it is. Without much more code there is no way to know where it comes from either. It can be hard to track these things down even when you can run the site (but I have some tips see below).

    When you convert a string like "foo" using intval it returns 0 for example. But you can bail on the function if the id is falsy For example:

      //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."
    ";
        //.....
     }
    
    addInOrder(1);
    addInOrder("foo");
    

    Output (returns on 0)

     1
    

    Sandbox

    This really isn't a solution, because you should find were the bad call is happening.

    function addInOrder($id){
        if(false ==($id = intval($id))){
            try{
               throw new Excepton(__METHOD__." called with invalid argument[$id]");
    
            }catch(Exception $e){
                 echo $e->getMessage()."
    ";
                 echo $e->getTraceAsString();
                 exit;
            }
        }
        echo $id."
    ";
     }
    

    Now when it happens, your site will explode (just kidding) but it will break, however you'll get a nice stacktrace you can use to find where the call came from so you can fix the actual problem.

    Basically for whatever reason you are getting 0 for the $id variable in your method. The most likely reason (besides you sending 0) is something other then a number is getting set to it and then converted to 0 with intval

    In $_SESSION ['products'] appears [0]

    If you call addInOrder("foo") for example you would get an array item with a number and an index of 0.

    I can't use $_SESSION in the sandbox but we can duplicate how it works using global $session. With that I can reproduce (what I think you are saying):

    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 "
    ---------------------------------
    ";
    addInOrder("foo");
    print_r($session);
    

    Output:

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

    Sandbox

    Personally I would throw an error whenever this method gets something that is falsy after intval just because it should never happen unless the call is messed up (where your not sending it an ID). But I should mention never show a stack trace on a production server as the arguments can contain things like the DB passwords etc...

    Hope it helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效