dongzhe3171 2013-11-04 12:05
浏览 47
已采纳

如何在Yii会话中更新数组的值

I am using "ajaxSubmitButton" to send some values of 3 fields: registrant, product id and quantity to controller (controller name: actionCart). After submitting the button I receive those values in session array what I did successfully. At this point, if I submit same product id but different quantity, I want to update the quantity key with new value. I have done this by php global $_SESSION but can't using Yii session variable.

public function actionCart()
{

    if(isset($_POST["Order"])){
        $item = $_POST["Order"];
        $registration_id = $item["registration_id"];
        $this->productId = $item["item"];
        $quantity = $item["quantity"];
        $quantity = $item["quantity"]=='' ? 1 : $item["quantity"];

        $productInfo = Products::model()->findByPk(array('id'=>$this->productId));
        $totalPrice = $productInfo->price * $quantity;

        $newItem = array("product_id" => $this->productId , "product_name" => $productInfo->name, "quantity" => $quantity,"price" => $productInfo->price,"totalPrice" => $totalPrice);

        $session = Yii::app()->session;
        $cartArray = $session['cart'];

        $searchArrResult = $this->searchSubArray($session['cart'],'product_id',$this->productId);
        if (!empty($searchArrResult)) {
            /***** this works *****/
            foreach ( $_SESSION['cart'] as $key=>$cart ) {
                if ( $cart["product_id"] == $this->productId ) {
                    $_SESSION['cart'][$key]['quantity']=$quantity;
                    $_SESSION['cart'][$key]['totalPrice']=$totalPrice;
                }
            }
            /***** following commented code does not work *****
             *
             foreach($session['cart'] as $key=>$cart){
                if ($cart["product_id"] == $this->productId){
                    $session['cart'][$key]['quantity'] == $quantity;
                    $session['cart'][$key]['totalPrice'] == $totalPrice;
                }
            }*/

        }
        else {
            $cartArray[] = $newItem;
            $session['cart'] = $cartArray;
        }

        print_r($session['cart']);
        //unset(Yii::app()->session['cart']);

    }

}

In the above code I marked by commenting where I want to update session values. Please help me someone if it is possible do in yii.

  • 写回答

1条回答 默认 最新

  • douyun1972 2013-11-04 12:23
    关注

    Try this:

    $carts = $session['cart'];
    foreach($carts as $key=>&$cart){
        if ($cart["product_id"] == $this->productId){
             $cart['quantity'] == $quantity;
             $cart['totalPrice'] == $totalPrice;
        }
    }
    $session['cart'] = $carts;
    

    Yii::app()->session return object of CHttpSession, not reference to $_SESSION.

    $carts = $session['cart'] equals operation $carts = $session->get('cart'); (by means magic method __get in CHttpSession) and $session['cart'] = $carts; equals to $session->set('cart', $carts); (by __set)

    That's why you can't setting by $session['cart'][$key]['quantity'] = $quantity;

    UPDATED full solution (I change logic of saving products - $key = product_id)

    public function actionCart()
    {
        if(isset($_POST["Order"])){
            $item = $_POST["Order"];
            $registration_id = $item["registration_id"];
            $this->productId = $item["item"];
            $quantity = empty(intval($item["quantity"])) ? 1 : intval($item["quantity"]);
            $productInfo = Products::model()->findByPk(array('id'=>$this->productId));.
            if(empty($productInfo))
                 return false; // or other action
            $newItem = array(
                 "product_id" => $this->productId , 
                 "product_name" => $productInfo->name, 
                 "quantity" => $quantity,
                 "price" => $productInfo->price,
                 "totalPrice" => ($productInfo->price * $quantity)
            );
            $cartArray = Yii::app()->session['cart'];
            $cartArray[$this->productId] = $newItem;
            Yii::app()->session['cart'] = $cartArray;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题