Welcome Guest, Not a member yet? Register   Sign In
unsetting session array
#1

[eluser]Yorkshire Pudding[/eluser]
Hi, i've been racking my brains all day with this one but have had no luck. I've search through alot of the previous posts but couldn't seem to find the answer.

I'm using CI Sessions for a simple shopping basket.
I set the data like this:
Code:
//check for items added to the basket and add to session

            if($this->input->post('pid'))
            {
                if($this->session->userdata('product_id'))
                {
                    $product_array = $this->session->userdata('product_id');
                }
                $product_array[] = $this->input->post('pid');
                $this->session->set_userdata('product_id', $product_array);
            }

Then I do a foreach on the 'product_id session variable', pulling the necessary info from the products database as it loops through.

What I want to allow the customer to do is to delete individual items from the basket. I know that the following deletes the entire session array:
Code:
$this->session->unset_userdata('product_id');
but how would i delete just one of the items from the array? Is there an easy way to do this or will I have to unset the array manually and then reset the 'product_id session'. An example print_r() of the 'product_id session array' gives something like array('0'=>'245', '1'=>'365', '2'=>456, '3'=>'824'). So how would I delete the product 365 with the array key 1?

I thought i might be able to do it in one line like:
Code:
$this->session->unset_userdata('product_id[1]');

Thanks for your help




Theme © iAndrew 2016 - Forum software by © MyBB