2010-05-07 40 views
0

由於某些原因,我很難理解CI中的會話如何工作。
我想更改下面的代碼部分以使用CodeIgniter會話,而不是通常在PHP中完成的方式。什麼是最好的方法來做到這一點?在CodeIgniter中使用會話庫

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

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

另一個問題!
在使用CI會話庫時,如果會話具有多維結構,在讀取我需要的值之前,是否必須首先將會話內容放入數組中?

回答

0

我會做這樣的

// 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); 
+0

感謝。這看起來很明智,我會放棄它。 – 2010-05-07 16:02:28