【问题标题】:zend_session goes wrong after get the second productzend_session 在得到第二个产品后出错
【发布时间】:2013-03-21 09:07:17
【问题描述】:

我是一个初学者zend。当我搞乱 zend_session 时,我遇到了这样的问题。

只有一个项目一切正常。 但不止一项,它会停止添加更多项或增加匹配项。

这是我的代码

// get value from form
            $intQuantity = $this->_request->getPost ( 'intQuantity' );
            $intSize = $this->_request->getPost ( 'intSize' );
            $intProductId = $this->_request->getParam('productId');
            //call model cart
            $cartModel = new Default_Model_cart();

            //input value into cartModel function addItem output array of matched row
            /*
             * Array
            *   (
            *       [0] => Array
            *           (
            *               [product_id] => 7
            *               [product_price] => 1600000
            *               [product_image_url_50] => 50_nike-janoski-new-1363618892.jpg
            *               [product_size_title] => 6.5
            *           )
            *   )
             * 
             * 
             * */
            $cartArrays = $cartModel->addItem($intProductId, $intQuantity, $intSize);

            //add quantity into cartArrays
            /*
             * Array
            *   (
                    *       [0] => Array
                    *           (
                            *               [product_id] => 7
                            *               [product_price] => 1600000
                            *               [product_image_url_50] => 50_nike-janoski-new-1363618892.jpg
                            *               [product_size_title] => 6.5
                            *               [quantity] => 1
                            *           )
                    *   )
            *
            *
            * */
            $cartArrays[0]['quantity'] = $intQuantity;
            //declare session
            $session = new Zend_Session_Namespace();
            /*
             * if true session->product got at least one array of of cartArray
             * */
            if(isset($session->product)) {
                $i=0;
                //foreach arrays into array
                foreach($session->product as $productSessionArray) {
                    /*if in an array got productId and product_size_title matched cartArrays productId and product_size_title
                    * add value from $intQuantity into appropriate $session->product determined by $i
                    */ 
                    if($productSessionArray['product_id'] == $cartArrays[0]['product_id'] && $productSessionArray['product_size_title'] == $cartArrays[0]['product_size_title']) {
                        $session->product[$i]['quantity'] += $intQuantity;
                        /*
                         * if values don't matched add the whole new array into $session->product
                         * */
                    } else {
                        $productSessionArrays[] = $productSessionArray;
                        $productSessionArrays[] = $cartArrays[0];
                        $session->product = $productSessionArrays;
                    }
                    //$i increase by 1
                    $i++;
            }
                //if there is not an array in $session added the array in to it
            } else {
                $session->product = $cartArrays;
            }

【问题讨论】:

    标签: zend-framework zend-session zend-session-namespace


    【解决方案1】:

    看起来您只是在测试第一个购物车商品的 id:

    if($productSessionArray['product_id'] == $cartArrays[0]['product_id'] && $productSessionArray['product_size_title'] == $cartArrays[0]['product_size_title'])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-29
      • 2013-10-27
      • 1970-01-01
      • 2022-09-24
      • 1970-01-01
      • 2020-05-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多