doutun9179 2013-01-25 11:07
浏览 18
已采纳

更新会话数组+ zf2的节点

First I added a session then I print my session it looks completely ok here it is

Array
(
[14] => Array//(main key is my restaurant Id)
    (
        [retaurantDetail] => Array
            (
                [restId] => 14
                [restaurantName] => Barca
                [published] => 1
                [timings] => 10 to 10
                [normalCost] => 150
                [logo] => 44f7afcffb0aeea5c69ccee9041cab84.jpg
                [email] => barca@barca.com
                [phone] => 741258
            )

        [menuArray] => Array
            (
                [70] => Array // (menu ID is the Key)
                    (
                        [menuId] => 70
                        [productId] => 35
                        [productName] => Coca Cola
                        [categoryTitle] => Beverages
                        [categoryId] => 52
                        [price] => 100
                        [attributeName] => 1.5L
                        [isDefault] => 1
                        [qty] => 1
                    )

            )

    )
)

Now I add some logic if some one add again that menu in add to cart in short he/she adds plus 1 qty to that menu here it is my code,(code is not completed yet but now I am focused on just to update qty)

            foreach($session->cartSession as $sessionKey=>$sessionVal)
            {

                        foreach($sessionVal['menuArray'] as $sessionMenuKey=>$sessionMenuVal)
                        {

                            if($sessionMenuKey == $post_data['menuId'])
                            {
                                echo"<pre>"; print_r($sessionMenuVal['qty']); echo "</pre>";
                                //$qty = $session->cartSession[$restaurantDetail['restId']]['menuArray'][$sessionMenuKey];
                                $sessionMenuVal['qty'] =  $sessionMenuVal['qty']+1;

                                echo"<pre>"; print_r($sessionMenuVal['qty']); echo "</pre>"; 

                            }

                         }
            }

What I am missing I want to updatre session qty.

every time I press add it shows 1 qty then I add plus 1 qty to it then it show 2 after then it again shows 1 qty :(.

  • 写回答

1条回答 默认 最新

  • duanna1407 2013-01-25 12:47
    关注

    You're never writing back into the Session...

    If i understand your code correctly, the following should work. Check the correct Array-Nesting of my code in case it doesn't work out of the box.

    // Somewhere on top, use this for your loops
    $cartSession = $session->offsetGet('cartSession'); 
    
    // Inside your matched loop
    $currentQty = $sessionMenuVal['qty'];
    $cartSession[$sessionKey][$sessionMenuKey]['qty'] = ++$currentQty;
    
    // After your loops at the end
    $session->offsetSet('cartSession', $cartSession);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划