I'm planning to make a dynamic checkbox form which binds each POST to a specific bitwise value, and adventually counts the total ammount of bitwise numbers. I cant think of any good method. could anyone guide me?
<html>
<head></head>
<body>
<form method="post" action="test.php"/>
<?php
$fields = array('writePost','readPost','deletePost','addUser',
'deleteUser');
$perms = array(
'writePost' => 1,
'readPost' => 2,
'deletePost' => 4,
'addUser' => 8,
'deleteUser' => 16
);
$results = array();
foreach($fields as $field)
{
echo "<input type='checkbox' name='".$field."' value='".$field."' />";
echo "<label for='".$field."'>".$field."</label>";
echo "<br/>";
}
if(isset($_POST['submit']))
{
foreach($_POST as $posts)
{
$posts = $results;
//
$total_key = 0;
$array_key = 1;
foreach ($perms as $key)
{
if (!$key=='read')
{
$array_key = $array_key*2;
}else{
$array_key = $array_key+$total_key;
}
foreach ($posts as $perms)
{
$array_key = $post;
}
}
}
var_dump($results);
}
var_dump($fields);
?>
<input type="text" name="name" value="name"/>
<input type="submit" name="submit" value="submit"/>
</form>
</body>
</html>
I think you want the values of your inputs to be the number instead of the field name:
echo "<input type='checkbox' name='nums[]' value='".$perms[$field]."' />";
After than you will want to start with the highest possible number and go backwards:
$n = sizeof($perms)-1;
$total = array_sum($_POST['nums']);
for($i=$n;$i>0;$i--)
{
$pow = pow(2, $i);
if ($pow > $total)
{
//$fields[$i] is checked
$total-=$pow;
}
}
But really, you could just put the checked values into an array and check if they are set in PHP if you want a less confusing way to do it.