开发者

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

开发者 https://www.devze.com 2023-03-18 09:53 出处:网络
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:

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

精彩评论

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