What's Here?
- Members: 149,084
- Replies: 505,721
- Topics: 79,649
- Snippets: 2,663
- Tutorials: 706
- Total Online: 1,439
- Members: 78
- Guests: 1,361
|
A script for converting binary and decimal values, for the purpose of storing information from a list of checkboxes.
|
Submitted By: Spider
|
|
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
/* The following is a basic example of a form you could use.
It is essential that the checkboxes are given numerical values which double each time beginning at 1. */
<form action="script.php" method="POST">
One: <input type=checkbox name=one value=1><br>
Two: <input type=checkbox name=two value=2><br>
Three: <input type=checkbox name=three value=4><br>
Four: <input type=checkbox name=four value=8><br>
Five: <input type=checkbox name=five value=16><br>
Six: <input type=checkbox name=six value=32><br>
Seven: <input type=checkbox name=seven value=64><br>
Eight: <input type=checkbox name=eight value=128><br>
<input type=submit value="Submit">
</form>
/* Then to get your decimal value you simply sum all of your checkbox values */
$total = $_POST['one'] + $_POST['two'] + $_POST['three'] + $_POST['four'] + $_POST['five'] + $_POST['six'] + $_POST['seven'] + $_POST['eight'];
/* Now the following is the code for retrieving the checkbox information from your decimal number */
/* This value is simply your stored decimal value */
$decval = 83;
/* If you wish to use more than 8 checkboxes, you need to change the number '8' in this line */
/* This will output 1 1 0 0 1 0 1 0
As our initial value in this example was 83 */
for($x = 0; $x < 8; $x++) {
}
/* To retrieve whether or not a specific checkbox was selected, say the 5th one,
you would retrieve $bitarray[4] (because arrays begin at 0!), with 1 being selected a 0 being not selected. */
Copy & Paste
|
|
|
Be Social
Reference Sheets
Bye Bye Ads
Monthly Drawing
Top Contributors
Top 10 Kudos This Month
|