我正在迭代地构建一个包含许多用户的用户信息的表,以及一个用于更新每个用户的状态字段的复选框。来自我的控制器的Psuedo代码:
foreach ($vols as $vol)
{
$data['user_name'][] = $vol->username;
$data['status'][] = '<input type="checkbox" name="signupStatus" /> confirm ';
}
$ data数组被传递到一个视图,在该视图中构建一个表,显示一个用户列表,每个用户旁边都有一个复选框。管理员可以通过用户名检查该框,以便更新其在数据库中的状态。
在我的提交函数中,如何构建包含用户名和关联状态(复选框值)的数组。
仅供参考:我正在使用Codeigniter,PHP5.2,MAMP
答案 0 :(得分:1)
foreach ($vols as $vol)
{
$rows[$i][] = //account info here
$rows[$i][] .= '<input type="checkbox" name="signupStatus['.$vol['id'].']" value = "0" /> confirm ';
// not sure where your user ID is, but I tried to guess :-)
}
// in receiving script:
// you will receive array of only those ids that were checked, so
$update_users = array_keys($_POST['signupStatus']);
// will give you an array of userids that were checked
当然你不应该忘记过滤它们,使它们只包含数字。像
foreach ($update_users as $key => &$userid) {
if ( ($userid = intval($userid)) <= 0 ) {
unset($update_users[$key]); // silently do nothing with anything we didn't expect
}
}