2013-03-19 68 views
1

我有用戶填充的codeigniter表單。在提交時將輸入發佈到數組。這個數組然後顯示在作爲變量加載的新視圖上。codeigniter - 在會話中存儲發佈數據並在視圖中顯示

我的問題是,如果我刷新視圖出於任何原因,這些職位會丟失,新視圖不能顯示變量。

如何將發佈的數組存儲爲會話值,然後如何訪問會話變量。

所以我的控制器:

function new_blank_order_lines() 
    { 
    $data = array(
    'customer' =>$this->input->post('customer'), 
    'period' =>$this->input->post('period'), 
    'buom' =>$this->input->post('buom') 
     ); 

    $this->load->view('sales/new_blank_order_lines',$data); 
    } 

new_blank_order_lines我可以echo $customer

顯示變量,如果我現在刷新視圖new_blank_order_lines,視圖被重載,交變量丟失。所以我想將$ data數組存儲爲一個會話,所以即使在頁面重新加載/刷新之後它仍然可用。

有什麼建議嗎?

我想是這樣的:

function new_blank_order_lines() 
     { 
     $data = array(
     'customer' =>$this->input->post('customer'), 
     'period' =>$this->input->post('period'), 
     'buom' =>$this->input->post('buom') 
      ); 

     $this->session->$data 

     $this->load->view('sales/new_blank_order_lines',$data); 
     } 

這顯然是行不通的?如果確實如此,我該如何迴應變量的值?

一如既往的感謝,

回答

4

你如何處理用戶提交給表單的信息?它沒有保存到數據庫或類似的?如果是這種情況,那你爲什麼不從中檢索數據?

如果你只是想能夠whow暫時將數據,你可以使用像會話變量在最後一個例子,但有一些修改:

function new_blank_order_lines() 
    { 
    $data = array(
    'customer' =>$this->input->post('customer'), 
    'period' =>$this->input->post('period'), 
    'buom' =>$this->input->post('buom') 
     ); 

    $this->session->set_userdata($data); 

    $this->load->view('sales/new_blank_order_lines',$this->session->all_userdata()); 
    } 

See user guide

+0

感謝您的支持。我只是在下一個表格完成後才寫入數據庫。所以只是想暫時顯示它。我按照上面的建議更改了會話的語法,並且在頁面首次加載時工作。如果我刷新頁面,會話變量也會丟失?這是與控制器有關嗎?也許它是重新加載數組與空白後變量?我如何做if語句來查看會話是否已經設置?還是我完全在錯誤的軌道上?謝謝 – Smudger 2013-03-19 09:11:08

+1

是的,如果沒有設置post變量,$ this-> input-> post()返回FALSE。所以請先檢查一下: if($ this-> input-> post()){ 這裏設置數據 – jonixj 2013-03-19 09:40:30

+0

傳說,謝謝! – Smudger 2013-03-19 10:29:56

0
$projectID = $this->input->post('projectID'); 
if($projectID!=''){ 
    $this->session->set_userdata('projectID', $projectID); 
} 

後數據不會在重新加載時使用此代碼