views:

30

answers:

1

For some reason I'm heaving a tough time understanding how sessions in CI work. I would like to change the following part of the code to use CodeIgniter sessions rather than the way it's normally done in PHP. What would be the best way to do it?

foreach($_POST['qty'] as $k => $v) { $id = (int)$k; $qty = (int)$v;

$_SESSION['cart'][$id]['quantity'] = $qty;

}

Another question! While using CI session library, when a session has multidimensional structure, do I always have to drop session's content to an array first, before I can read the values I need?

A: 

I'd do it like this

// Fetch the cart from the session
$cart = $this->session->userdata('cart');

// Update the cart
foreach ($this->input->post('qty') as $k => $v)
{
    $id = (int) $k;
    $qty = (int) $v;

    $cart[$id]['quantity'] = $qty;
}

// Save the cart back to the session
$this->session->set_userdata('cart', $cart);
Stephen Curran
Thanks. That looks sensible, I'll give it a go.
marcin_koss

related questions