2012-10-17 79 views
0

我的頁面上有一個表單。在「提交」我需要從窗體中獲取發佈數據並將其存儲在多維數組中。CodeIgniter - 將_post數據存儲在多維數組中並顯示

這是我到目前爲止。

//view 
<input type="text" name="option[fname]" /> 
<input type="text" name="option[lname]" /> 
<input type="submit" /> 

//controller 
$data['myarray'] = $this->input->post('options', TRUE); 
$this->load->view('template',$data); 

然後在「提交」我們回到視圖,看看我們添加了什麼。

//view 
<?php 
foreach($myarray as $row){ 
    echo $myarray['fname'] . '<br>' . $myarray['lname']; 
} 
?> 

我希望每次點擊「提交」時都可以繼續添加到數組中。現在它只顯示最後輸入的數據。我怎麼做?謝謝?

回答

0

您必須在每次請求時將數組保存在文件,cookie或數據庫中。

與儲蓄簡化的例子到文件:

// controller 
// load array from the file if exists 
if (is_file('myarray.dat')) $myarray = unserialize(file_get_contents('myarray.dat')); 
// put new data to array 
$myarray[] = $this->input->post('options', TRUE); 
// output array 
$this->load->view('template', array('myarray' => $myarray)); 
// save array back to the file 
file_put_contents('myarray.dat', serialize($myarray));