如何使用Codeigniter-3中的会话将多个产品存储在添加到购物车中

时间:2019-02-04 17:03:42

标签: codeigniter-3

当用户单击“添加到购物车”按钮以及如何使用codeigniter从SESSION获取所有产品时,我想在SESSION中添加多个产品...请帮助。

if ($_POST) 
        {
            $sessionData = [
                    'p_id' => $this->input->post('pid'),
                    'p_name' => $this->input->post('pname'),
                    'p_qty' =>$this->input->post('viewproduct_details_qty'),
                    'p_price' => $this->input->post('pprice'),
                    'p_discount' => $this->input->post('pdiscount'),
                    'p_dprice' => $this->input->post('pdiscountprice'),
                    'p_imagename' => $this->input->post('pimagename'),
                ];

            echo "<pre>";    
            print_r($sessionData);

            $p_session = $this->session->set_userdata($sessionData);
            //$this->load->view("add_to_cart_page_session");    

        }

1 个答案:

答案 0 :(得分:0)

请使用以下代码:

首先,您必须在 config-> autoload.php

中加载会话库。
$autoload['libraries'] = array('session'); // Load session Library in autoload.php


if ($_POST) 
        {
            $sessionData = [
                    'p_id' => $this->input->post('pid'),
                    'p_name' => $this->input->post('pname'),
                    'p_qty' =>$this->input->post('viewproduct_details_qty'),
                    'p_price' => $this->input->post('pprice'),
                    'p_discount' => $this->input->post('pdiscount'),
                    'p_dprice' => $this->input->post('pdiscountprice'),
                    'p_imagename' => $this->input->post('pimagename'),
                ];

            echo "<pre>";    
            print_r($sessionData);

            $this->session->set_userdata('YOUR_SESSION_NAME',$sessionData); // You have to give session name
             print_r($this->session->userdata('YOUR_SESSION_NAME')); // You Can Access Session by name

            //$this->load->view("add_to_cart_page_session");    

        }