循环遍历复选框-PHP


Looping over checkboxes -PHP

我有一个大约有50个输入复选框的页面,其中

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个表格我要评论美元= '这个学生 ...................................................(所有50个表单)'

任何链接或提示将不胜感激。

您正在用=而不是连接.=覆盖每次迭代中的值,但我相信您可以使用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;
}

$givenforms = ', '.$forms.'';是错误的,因为每运行一次循环都会覆盖前一次。
使用.=(连接操作符)代替=

还要确保在使用$givenforms .= ...........

连接变量之前在循环外使用$givenforms = "";来设置变量

如果你不这样做,你会得到一个警告(或通知,不确定)。