0
$data=array();
$data[]= array('product_id'=>$this->input->post('product_id'),
                'quantity'=>$this->input->post('quantity'),
                'unit'=>$this->input->post('unit'),
                'unit_rate'=>$this->input->post('unit_rate'));

                   $this->session->set_userdata('data',$data);
               $post_array['cart'][]=$this->session->userdata('data');

I want to append $data to $post_array. How do i do it with codeigniter??

I am using this code for creating shopping cart where I want to store each product added by customer to session. And want to display all product that was added by user in separate table.

3
  • 1
    Just simple try to assign it: $post_array['cart'][] = $data Commented Dec 30, 2014 at 12:18
  • and then?? How Do I access it using foreach loop?? Commented Dec 30, 2014 at 12:20
  • this is going to be difficult at every step. my suggestion is to make a database table for the cart. when the first item is chosen, create a unique ID for the user and store that ID in the session. use that Unique ID in the cart table. new page - retrieve the Unique ID from session, use the Unique ID to return the items in the cart table. Commented Dec 30, 2014 at 22:25

1 Answer 1

2

You can just simple assign your data to your cart array.

$post_array['cart'][] = $data;

Later, if you want to loop through that you can use this:

foreach ($post_array['cart'] as $item) {
    echo 'Id of product: ' . $item['product_id']."<br />";
    echo 'Quantity: ' . $item['quantity']."<br />";
    //and so on...
}

But I am assume, you want to add it to the session, not for the post.

EDIT

Base on OP comment. You are always overwriting your $_SESSION['data'] variable. So add it as a new array:

//Set the data
$data[] = array('product_id' => $this->input->post('product_id'),
    'quantity' => $this->input->post('quantity'),
    'unit' => $this->input->post('unit'),
    'unit_rate' => $this->input->post('unit_rate'));
//Get the cart
$cart = $this->session->userdata('data');
//Add data to this temporary variable
$cart[] = $data;
//Set back the data
$this->session->set_userdata('data', $cart);

//Later, when you want to iterate through the cart:
foreach ($this->session->userdata('data') as $item) {
    echo 'Id of product: ' . $item['product_id'] . "<br />";
    echo 'Quantity: ' . $item['quantity'] . "<br />";
    //and so on...
}
Sign up to request clarification or add additional context in comments.

4 Comments

You are right I want to add it in session. And right now I am exactly doing what you said, bit problem is that when user adds another product, second product row replaces first product row..
I tried it, but it again replacing old row with new one. Would i show you my full code?
Oh yes, that was my mistake. I've edited my code. Change this $this->session->set_userdata('data', $data); to this $this->session->set_userdata('data', $cart); I've added the data, what is only 1 item, instead the newly created cart.
I tried it, but now values are getting blank..Means rows are inserting but but with blank values

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.