gpt4 book ai didi

php - 如何使用复选框值的结果创建 id 字段数组

转载 作者:行者123 更新时间:2023-11-29 14:47:37 24 4
gpt4 key购买 nike

我正在迭代地构建一个表,其中包含多个用户的用户信息以及用于更新每个用户的状态字段的复选框。来 self 的 Controller 的伪代码:

foreach ($vols as $vol)
{
$data['user_name'][] = $vol->username;
$data['status'][] = '<input type="checkbox" name="signupStatus" /> confirm ';
}

$data 数组被传递到一个 View ,其中构建了一个表,显示用户列表,每个用户旁边都有一个复选框。管理员可以通过用户名选中该框,以便更新其在数据库中的状态。

在我的提交函数中,如何构建一个包含用户名和关联状态(复选框值)的数组。

仅供引用:我正在使用 Codeigniter、PHP5.2、MAMP

最佳答案

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
}
}

关于php - 如何使用复选框值的结果创建 id 字段数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6589204/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com