简体   繁体   中英

codeigniter array data in session

I started to learn codeigniter and now I have one prblem. I am making online store for laptops and now I have a wish to make a cart. I wanna remember all data in session when user add some laptop in cart, redardless is logged or not. But my problem is that I remember in session only last added id of laptops. I managed to remember number of laptops in total, and total price, but can't manage how to remember all id. I am using next code in controller:

public function adding()
{
    $id_lap=$this->input->post('id_lap');   // id laptop of which was clicked by user
    $num_items=$this->input->post('num_items');  // total number of laptops
    $price=$this->input->post('price');  // total cost

     if($id_lap !='')
     {      
             $this->session->set_userdata('num_items',$num_items);
             $this->session->set_userdata('price_new',$price);
             $this->session->set_userdata('id_lap_new',$id_lap);

             $response=array(
                'status'=>1,
                'num_items'=>$num_items,
                'price_new'=>$price,
                'id_new_lap'=>$id_lap
             );   

     } else {

         $response=array(
            'status'=>0
         );
     }

     echo json_encode($response);
}

I am using Ajax for sending data in controller.

Thanks for answer, I appreciate.

try to save the session in the database, first open your config.php under application folder and change use session database option value to true. Import the sql structure like you have there and use sessions like you have. It will store the session variable in db. You might want to enable session encryption as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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