2013-04-06 47 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