2013-01-22 19 views
0

在對我的看法,我有以下形式:從陣列獲取的數據是無效的

<table align="center" border="1"> 
<tr> 
    <td>Identificação</td> 
    <td>Conc, %</td> 
    <td>Classificação 67/548/CEE</td> 
    <td>Classificação 1272/2008 (CLP)</td> 
</tr> 
<tr> 
    <td> 
     <textarea rows="4" cols="30" name="componentes[0][identificacao]"></textarea> 
    </td> 
    <td> 
     <textarea rows="4" cols="30" name="componentes[0][conc]"></textarea> 
    </td> 
    <td> 
     <textarea rows="4" cols="30" name="componentes[0][classificacao_cee]"></textarea> 
    </td> 
    <td> 
     <textarea rows="4" cols="30" name="componentes[0][classificacao_clp]"></textarea> 
    </td> 
</tr> 
</table> 

<div id="outro_curso"></div> 
<p class="submit"> 
    <button id="novo_curso">Add Curso</button> 
</p> 

正如你所看到的,我使用的是多維數組。我這樣做,因爲當我點擊「添加Curso」按鈕時,我調用一個jquery函數來生成另一個表,就像上一個表一樣。文本區域的名稱將是componentes[1][identificacao],componentes[1][conc]等...

注意:我可以使用該按鈕來生成我想要的表的次數。

現在,我的問題是如何讓我的CodeIgniter模型中處理這些數據。我試圖將數據保存到$componentes陣列(稍後插入到數據庫中),但我想有什麼錯我的代碼:

$componentes; 
    foreach ($this->input->post('componentes') as $key => $value){ 
     $componentes[$key]['identificacao']=$value[$key]['identificacao']; 
     $componentes[$key]['conc']=$value[$key]['conc']; 
     $componentes[$key]['classificacao_cee']=$value[$key]['classificacao_cee']; 
     $componentes[$key]['classificacao_clp']=$value[$key]['classificacao_clp']; 
    } 

有人可以給我一點幫助嗎?

編輯:

我忘了提,我得到的錯誤:

Invalid argument supplied for foreach().

所以我不知道如果我的foreach ($this->input->post('componentes') as $key => $value){是正確的,或者是在線路的一些問題內。

+0

從快看那個元素就快到了,我猜你需要這個:'$ componentes [$ key] ['identificacao'] = $ value ['identificacao'];' – Mudshark

+0

我編輯了我的帖子Mudshark。現在我無法測試您的建議,因爲我的foreach出現錯誤。 – user1511579

回答

1
$componentes_post = $this->input->post('componentes'); 
if(is_array($componentes_post)&&!empty($componentes_post)) 
{ 
    foreach ($componentes_post as $key => $value) 
    { 
     $temp_componentes['identificacao']=$value['identificacao']; 
     $temp_componentes['conc']=$value['conc']; 
     $temp_componentes['classificacao_cee']=$value['classificacao_cee']; 
     $temp_componentes['classificacao_clp']=$value['classificacao_clp']; 
     $componentes[] = $temp_componentes; 
    } 
} 

現在看,

print_r($componentes); 

你應該得到你所需要的。 接下來,你正在爲的foreach(提供)錯誤無效的參數,因爲你已經在foreach迭代數組爲空,確保值是通過使用print_r($this->input->post('componentes'))

+0

不能要求更好。非常感謝,現在一切正常。 – user1511579