note 70728 added to function.pow

From: Date: Wed, 25 Oct 2006 15:02:01 +0000
Subject: note 70728 added to function.pow
Groups: php.notes 
Request: Send a blank email to php-notes+get-119103@lists.php.net to get a copy of this message
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"> ---- Server IP: 217.13.201.10 Probable Submitter: 84.57.20.14 ---- Manual Page -- http://www.php.net/manual/en/function.pow.php Edit -- https://master.php.net/note/edit/70728 Del: integrated -- https://master.php.net/note/delete/70728/integrated Del: useless -- https://master.php.net/note/delete/70728/useless Del: bad code -- https://master.php.net/note/delete/70728/bad+code Del: spam -- https://master.php.net/note/delete/70728/spam Del: non-english -- https://master.php.net/note/delete/70728/non-english Del: in docs -- https://master.php.net/note/delete/70728/in+docs Del: other reasons-- https://master.php.net/note/delete/70728 Reject -- https://master.php.net/note/reject/70728 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#119103) next »