2013-04-06 41 views
1

我有大約50輸入複選框一個頁面,其中他們循環執行復選框-php

name="form[]" 

我需要我的PHP腳本能夠遍歷所有的複選框和以往那些被檢查並提交我需要放入一個變量,所以我可以存儲哪些形式給定。

這是我的循環:

if ($this->input->post('action') == 'additional') { // Checks if radio button was checked 
    foreach ($this->input->post('form') as $forms) { // If above ^^^ is true loop through form[] 
     $givenforms = ', '.$forms.''; // What ever form[] is in the array assign them to the givenforms variable 
    } 
    $comments = 'This student was given'.$givenforms.''; // The comment I want to store in the database 
} 

這工作,但對於只有一種形式(複選框)。如果由於某種原因,我的客戶需要給學生全部50份表格,我希望$ comment ='這個學生被授予......................... ..........................(全部50份表格)'

任何鏈接或小費將不勝感激。

+0

這是CodeIgniter的權利?最好將框架添加到標籤中。 – 2013-04-06 17:59:44

+0

哦,真抱歉!我現在就這樣做! – 2013-04-06 18:02:45

+0

也只是爲了增加,兩個答案產生相同的確切結果! Fabricio剛剛回答。 – 2013-04-06 18:07:57

回答

4

你在每次迭代與=覆蓋值,而不是串聯.=,但我相信你可以使用implode爲您的使用情況:

if ($this->input->post('action') == 'additional') { // Checks if radio button was checked 
    $givenforms = implode(', ', $this->input->post('form')); 
    $comments = 'This student was given'.$givenforms; 
} 
+0

這個答案是正確的! – 2013-04-06 18:02:11

1

$givenforms = ', '.$forms.'';是錯誤的,每個東陽通過循環運行將覆蓋以前。
使用.=(連接運算符)而不是=

還要確保使用$givenforms = "";外循環使用$givenforms .= ...........

如果你不這樣做,你會得到一個警告(或通知,不知道)串聯之前設置變量。

+0

這個答案是正確的! – 2013-04-06 18:01:11

+0

Concatenate是您的'追加運算符'的正確詞語 – 2013-04-06 18:19:07

+0

我改變了這一點,但Rixhers在同一時間對其他內容進行了編輯。 – Lukas 2013-04-06 19:04:23