简体   繁体   English

在会话中从购物车中删除项目 | 代码点火器

[英]Removing item from Cart in Session | Codeigniter

I have items in my cart with session and I want to remove the item, my nested array is as follows,我的购物车中有带有会话的项目,我想删除该项目,我的嵌套数组如下,

[cart_items] => Array
        (
            [0] => Array
                (
                    [item_id] => 1
                    [item_name] => Neapolitan Pizza
                    [price] => 750.00
                    [toppings] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 3
                                    [name] => Chilli
                                    [price] => 100.00
                                )

                            [1] => Array
                                (
                                    [id] => 4
                                    [name] => Green chilli 
                                    [price] => 100.00
                                )

                        )

                    [quantity] => 1
                    [price_per_item] => 750
                )

            [1] => Array
                (
                    [item_id] => 16
                    [item_name] => Cheesy Garlic Bread
                    [price] => 420.00
                    [quantity] => 1
                    [price_per_item] => 420
                )

        )

Controller Code:-控制器代码:-

In my controller, I have created the following function to remove each item .在我的控制器中,我创建了以下函数来remove each item what am I missing here?我在这里错过了什么?

/Cart/remove /购物车/移除

public function remove($id){
        $this->load->library('session');
        $list = $this->session->userdata('cart_items');
        if($list){
            foreach ($list as $item=>$value){
                if($id==$value['item_id']){
                    $this->session->unset_userdata($value);
                }
            }
        }
        redirect('Cart/index');

    }

In my view, this is how I pass the item id in the anchor tag.在我看来,这就是我在锚标记中传递项目 ID 的方式。

View:-看法:-

<?php $list = $this->session->userdata('cart_items');
        $grand_total=0;
        if($list==Null) {
            print_r("<div><li class='list-group-item d-flex justify-content-between lh-condensed'>There are no items in the your cart</li></div>");
            $sub_total=0;
        }else{
            $sub_total=0;
            foreach ($list as $item=>$value){
                echo "<li class='list-group-item d-flex justify-content-between lh-condensed'>";
                if($value['item_id']<16) {

                    print_r("<div>" . $value['item_id'] . "</div>");
                    print_r("<div>" . $value['item_name'] . "</div>");
                    print_r("<div>" . $value['price'] . "</div>");

                    // toppings array is empty 
                    $toppings_price = 0;
                    if ($value['toppings'] == null) {
                        print_r("<div>No toppings selected</div>");
                    } else {
                        foreach ($value['toppings'] as $val) {
                            // printing topping names
                            print_r("");
                            print_r($val['name']);
                            print_r(",");
                            $toppings_price = $toppings_price + $val['price'];
                        }
                    }

                }else{
                    print_r("<div>" . $value['item_id'] . "</div>");
                    print_r("<div>" . $value['item_name'] . "</div>");
                    print_r("<div>" . $value['price'] . "</div>");
                }

                print_r("<div>".$value['quantity']."</div>");
                print_r("<div><a href='".base_url()."index.php/Cart/remove/".$value['item_id']."' class='btn btn-danger'>Remove</a></div>");
                echo "</li>";

                //sub total
                if($value['item_id']<16){
                    $sub_total+=($value['price']*$value['quantity'])+$toppings_price;
                }else{
                    $sub_total+=($value['price']*$value['quantity']);
                }
            }
        }
        $grand_total = $sub_total;
        ?>

When i clicked on the "remove" button it loads to the same page but item row doesn't get removed.当我单击“删除”按钮时,它会加载到同一页面,但不会删除项目行。

public function remove($id){
        $this->load->library('session');
        $list = $this->session->userdata('cart_items');
        if($list){
            foreach ($list as $item=>$value){
                if($id==$value['item_id']){
                    $this->session->unset_userdata($value['item_id']);
                }
            }
        }
        redirect('Cart/index');

    }

View Code:-查看代码:-

<td>
<a href="<?= base_url().'user/remove/'.$value['item_id']; ?>">
  <button>REMOVE</button>
</a>
</td>

destroy or unset the value of the session?销毁或取消设置会话的值?

 $this->session->unset_userdata('some_name');

and for multiple data you can:-对于多个数据,您可以:-

$array_items = array('username' => '', 'email' => '');

$this->session->unset_userdata($array_items);

and to destroy the session:-并销毁会话:-

$this->session->sess_destroy();

Note:- For more information about Session Class in Codeigniter,注意:-有关 Codeigniter 中会话类的更多信息,

https://codeigniter.com/userguide3/libraries/sessions.html https://codeigniter.com/userguide3/libraries/sessions.html

That's because your userdata is not the values themselves but the array containing them.那是因为您的 userdata 不是值本身,而是包含它们的数组。 You need to remove the item from the array and then save the array as the new userdata.您需要从数组中删除该项目,然后将数组保存为新的用户数据。 Try something like this:尝试这样的事情:

public function remove($id){
  $this->load->library('session');
  $list = $this->session->userdata('cart_items');

  if($list){
    foreach ($list as $item=>$value){
      if($id==$value['item_id']){
        unset($list[$item]);
        $list = array_values($list); // optional, reindex the array.
        break;
      }
    }

    $this->session->set_userdata('cart_items', $list);
  }

  redirect('Cart/index');

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM