Checking for marked checkbox  
Author Message
bob.herbst





PostPosted: 5/12/2006 10:35:06 PM Top

php-general, Checking for marked checkbox When cycling through a MySQL database I made a table the first column
of the table I made a checkbox so that if a user checks that checkbox
they can modify the information in that row. I don't know if this is
the best way to present a user information so that they can edit it.

Anyway, these checkboxes are labelled user[0], user[1] and so on. I was
wondering if there would be an easy way to check to see if any or all
of these are checked without manually doing if...else statements for
each one.

Thanks

 
David Haynes





PostPosted: 5/12/2006 11:14:00 PM Top

php-general >> Checking for marked checkbox email***@***.com wrote:
> When cycling through a MySQL database I made a table the first column
> of the table I made a checkbox so that if a user checks that checkbox
> they can modify the information in that row. I don't know if this is
> the best way to present a user information so that they can edit it.
>
> Anyway, these checkboxes are labelled user[0], user[1] and so on. I was
> wondering if there would be an easy way to check to see if any or all
> of these are checked without manually doing if...else statements for
> each one.
>
> Thanks
>
If I understand you correctly, you want to know if a set of $_POST
values has one or more set to '1'.

If so, I think this will show one way:
<?php
$tests[] = array(0, 0, 0, 1, 0, 1);
$tests[] = array(0, 0, 0, 0, 0, 0);

foreach( $tests as $test ) {
print_r($test);

$on = 0;
foreach( $test as $t ) {
$on |= $t;
}

if( $on == 0 ) {
printf("no values are on\n");
} else {
printf("at least one value is on\n");
}
}
?>

-david-