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.

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

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题