Skip to content

Instantly share code, notes, and snippets.

@RalfAlbert
Created February 11, 2012 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RalfAlbert/1799597 to your computer and use it in GitHub Desktop.
Save RalfAlbert/1799597 to your computer and use it in GitHub Desktop.
Handling multiple checkboxes in an easy way
<?php
$mPost = array(
'action' => 'send',
'user' => 'Horst',
'a' => 'On',
'c' => 'On',
'foo' => 'bar',
'baz' => '',
);
$defaults = array(
'a' => '',
'b' => '',
'c' => '',
);
$data = array_merge( $defaults, $mPost );
$bin = '';
foreach( $defaults as $key => $val ){
$bin .= sprintf( '%b', !!$data[$key] );
//$bin .= ! empty( $data[$key] ) ? '1' : '0';
}
$dec = bindec( $bin );
var_dump( $bin );
var_dump( $dec );
switch( $bin ){
case '000':
//nichts ausgewaehlt
break;
case '100':
// nur checkbox 'a' wurde ausgewählt
break;
case '101':
// checkbox 'a' wurde ausgewaehlt, checkbox 'b' jedoch NICHT
break;
default:
// alle anderen Faelle
break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment