Code Snippets

  

PHP Source Code


Welcome to Dream.In.Code
Become a PHP Expert!

Join 149,084 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,439 people online right now. Registration is fast and FREE... Join Now!





Checkbox list storing

A script for converting binary and decimal values, for the purpose of storing information from a list of checkboxes.

Submitted By: Spider
Actions:
Rating:
Views: 9,246

Language: PHP

Last Modified: June 23, 2006
Instructions: The code is set up for 8 checkbox values, but you can increase the number by changing the relevant parts of the code (read the comments for specifics).

The intention of this code is that you can store a decimal value in a database which will contain information on which checkbox options have been selected and which haven't. In order for it to work you need to have a fixed order for your checkbox options which will not change!

The code is in two halves, first of all is the data input, where you select the checkboxes and store the decimal value. The second part is retrieving information about the checkboxes.

Snippet


  1. /* The following is a basic example of a form you could use.
  2.    It is essential that the checkboxes are given numerical values which  double each time beginning at 1. */
  3. <form action="script.php" method="POST">
  4. One: <input type=checkbox name=one value=1><br>
  5. Two: <input type=checkbox name=two value=2><br>
  6. Three: <input type=checkbox name=three value=4><br>
  7. Four: <input type=checkbox name=four value=8><br>
  8. Five: <input type=checkbox name=five value=16><br>
  9. Six: <input type=checkbox name=six value=32><br>
  10. Seven: <input type=checkbox name=seven value=64><br>
  11. Eight: <input type=checkbox name=eight value=128><br>
  12. <input type=submit value="Submit">
  13. </form>
  14.  
  15. /* Then to get your decimal value you simply sum all of your checkbox values */
  16. $total = $_POST['one'] + $_POST['two'] + $_POST['three'] + $_POST['four'] + $_POST['five'] + $_POST['six'] + $_POST['seven'] + $_POST['eight'];
  17.  
  18.  
  19. /* Now the following is the code for retrieving the checkbox information from your decimal number */
  20.  
  21. /* This value is simply your stored decimal value */
  22. $decval = 83;
  23.  
  24. /* If you wish to use more than 8 checkboxes, you need to change the number '8' in this line */
  25. $binstring = strrev(str_pad(decbin ($decval),8,"0",STR_PAD_LEFT));
  26. $bitarray = explode(":",chunk_split($binstring, 1, ":"));
  27.  
  28.  
  29. /* This will output 1 1 0 0 1 0 1 0
  30.    As our initial value in this example was 83 */
  31. for($x = 0; $x < 8; $x++) {
  32.  echo $bitarray[$x]." ";
  33. }
  34.  
  35. /* To retrieve whether or not a specific checkbox was selected, say the 5th one,
  36.    you would retrieve $bitarray[4] (because arrays begin at 0!), with 1 being selected a 0 being not selected. */

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.




Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month