开发者

How to create array of id fields with result of checkbox value

I'm iteratively building a table that includes user information for a number of users, and a checkbox to update a status field for each user. Psuedo-code from my controller:

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

The $data array is passed to a view where a table is built showing a list of users with a checkbox next to each. An admin can check the box by a user name in order to update their status in the database.

In my submit function how can I build an array that contains the username and the associated status (checkbox value).

FYI: I'm using 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

Of course you should not forget to filter them so that they contained only digits. Like

foreach ($update_users as $key => &$userid) {
    if ( ($userid = intval($userid)) <= 0 ) {
        unset($update_users[$key]);    // silently do nothing with anything we didn't expect
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜