note 70728 deleted from function.pow by colder
| From: | colder@php.net | Date: | Thu, 26 Oct 2006 10:44:10 +0000 |
| Subject: | note 70728 deleted from function.pow by colder | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-119133@lists.php.net to get a copy of this message | ||
Note Submitter: gerrit dot boettcher at gmail dot com
----
Here is a simple example to calculate the numbers used, by a aglorythmus based checkbox.
You got Checkboxes like:
<input type="checkbox" name="option1" value="1">
<input type="checkbox" name="option2" value="2">
<input type="checkbox" name="option3" value="4">
<input type="checkbox" name="option4" value="8">
<input type="checkbox" name="option5" value="16">
<input type="checkbox" name="option6" value="32">
<input type="checkbox" name="option7" value="64">
<input type="checkbox" name="option8" value="128">
In the Database you got only one (int) field.
So you sum up the checkboxes submited for example:
option1 + option3 + option6 = 37.
To trace back which options are checked you now use the function..
<?php
function traceback ($int) {
$return = false;
$numbers = array();
do {
$tmp = floor(log($int, 2));
$numbers[] = pow(2, $tmp);
$int -= end($numbers);
if ($int == 0) {
$return = true;
}
} while($return == false);
return $numbers;
}
$checked_options = traceback(37);
print_r($checked_options);
/*
Array
(
[0] => 32
[1] => 4
[2] => 1
)
*/
?>
So you can simply reassign the numbers to the checked checkboxes via in_array...
<input type="checkbox" value="1" <?=(in_array(1,
$checked_options)?"checked":"")?> name="option1">