【问题标题】:Removing array item from session in CakePHP从 CakePHP 的会话中删除数组项
【发布时间】:2018-01-20 23:12:40
【问题描述】:

我在会话中添加了一个数组(购物车)。现在要从购物车中删除一个项目,我尝试了这个逻辑。购物车工作正常,我可以在我的页面上看到购物车中的所有物品。删除逻辑没有给我任何错误,但也没有从会话中删除项目。

我做错了什么?

function addToCart(){

    $this->layout = false;
    $this->render(false);

    $cart = array();

    $tempcart = unserialize($this->Session->read("cart")); 

    if(isset($tempcart)){
        $cart = $tempcart;
    }

    $productId = $this->request->data("id");

    if(!$this->existsInCart($cart, $productId)){

        $cart[] = array("productId" => $productId, "createdAt" => date());

        $this->Session->write("cart", serialize($cart));

        echo "added";
    }
    else
        echo "duplicate";
}


function removeFromCart(){

    $this->layout = false;
    $this->render(false);

    $cart = array();

    $tempcart = unserialize($this->Session->read("cart"));

    if(isset($tempcart)){
        $cart = $tempcart;
    }

    $productId = $this->request->data("productId");

    for($i=0;$i<count($cart);$i++){

        $cartItem = $cart[$i];   //  an array

        if($cartItem["productId"]==$productId)
            unset($cart[$i]);
    }

    $this->Session->write("cart", serialize($cart));

    echo "removed";
}

【问题讨论】:

  • 您是否在 $productId 中获得了一些值?还是$id??
  • @KuldeepChoudhary 智能捕捉!
  • 实际上在表中它是 id,我点击一个产品并发送 productId 中的 id 值。正如我在谷歌浏览器开发者工具中检查的那样,它的工作原理

标签: arrays session cakephp


【解决方案1】:

您没有使用正确的值更新会话

function removeFromCart() {
    $this->layout = false;
    $this->render(false);

    $productId = $this->request->data("productId");

    // make sure this is the value you need
    debug($productId);

    $tempCart = unserialize($this->Session->read("cart"));
    if (!empty($tempCart)) {
        for ($i=0; $i<count($tempCart); $i++) {
            if ($tempCart[$i]["productId"] == $productId) {
                unset($tempCart[$i]);
            }
        }
        $this->Session->write("cart", serialize($tempCart));
    }

    echo "removed";
}

【讨论】:

    【解决方案2】:
    $productId = $this->request->data("productId");
    

    您确定要写“productId”而不是“id”吗?

    您似乎在添加到购物车期间在请求中发送了“id”,可能您在删除时也这样做了。

    另外,您的购物车保存在$cart,因此您需要序列化$cart 而不是$newcart

    因此,您从购物车中移除的代码变为:

    function removeFromCart(){
    
        $this->layout = false;
        $this->render(false);
    
        $cart = array();
    
        $tempcart = unserialize($this->Session->read("cart"));
    
        if(isset($tempcart)){
            $cart = $tempcart;
        }
    
        $productId = $this->request->data("id");
    
        for($i=0;$i<count($cart);$i++){
    
            $cartItem = $cart[$i];   //  an array
    
            if($cartItem["productId"]==$productId)
                unset($cart[$i]);
        }
    
        $this->Session->write("cart", serialize($cart));
    
        echo "removed";
    }
    

    【讨论】:

      【解决方案3】:

      为什么你不只使用会话组件的delete() 函数?

      $this->Session->delete('cart');
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-10-08
        • 1970-01-01
        • 1970-01-01
        • 2016-12-10
        • 2017-07-25
        • 1970-01-01
        • 2011-04-02
        相关资源
        最近更新 更多