0

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 ($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

3
  • In your example code, there are no userids shown. How does a user-id is related to a checkbox? Commented Jul 5, 2011 at 21:42
  • Maybe if you explain what you're trying to do. Why would the value of your checkbox be 0? Why is the HTML for the checkbox part of the array at all? Commented Jul 5, 2011 at 21:46
  • @hakre @Cfreak - The actual use case is much more convoluted than is probably helpful - perhaps this clarifies what I'm trying to do? Commented Jul 5, 2011 at 21:59

1 Answer 1

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

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
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.