Jump to content

[SOLVED] Multiple Checkbox Binary Storage.


vulcandth

Recommended Posts

Hello, I have an inquery I hope someone here may know how I might do what i'm trying to do.

I have my own personal php script that acts as a command list for my clan. Recently I've integrated it into my vbulletin forums to allow people to click on their names to take them to their userprofile and it also pulls the join date from the boards and shows it on the command list. On the forums we have added a custom profile field that list a series of checkboxes and asks the user what games he/she plays and they check them. I want to be able to pull that information from vbulletin and display it on my command list. I started with a guess and check method to determine how vbulletin stores the information into the database. Basically this is how it works.

 

Checkbox1 = 1

Checkbox2 = 2

Checkbox3 = 4

Checkbox4 = 8

Checkbox5 = 16

Checkbox6 = 32

...

 

If you check Say... Checkbox1, Checkbox4, and Checbox6. It does the following.

1 + 8 + 32 = 41

And 41 would be what is stored into the Database.

 

So I have a very Rudementary Understanding of how the information is stored, now I just need to figure out a way to retrieve the information.

 

When I did more research on this method I believe it is a Binary system... Correct me if i'm wrong.

Has anyone have any advice or help they can offer I would be much appreciated.

 

If anyone wants to see the command list you may go to this page....

http://www.dominionwarriors.net/v2/index.php?pageid=Command

 

If you wish to see any code just ask for what you want to see. Thanks all =]

Link to comment
https://forums.phpfreaks.com/topic/138275-solved-multiple-checkbox-binary-storage/
Share on other sites

A good way to do it is to set up constants for your settings

 

SET_1 = 1

SET_2 = 2

SET_3 = 4

SET_4 = 8

 

etc...

 

and then when you combine the settings you bitwise OR them together...

 

<?php
$userSettings = SET_1 | SET_3; // A user that has setting 1 (0001) and setting 3 (0100)
// $userSettings is set to binary 0101
?>

 

And later when you wish to check to see if they have the setting, use a bitwise AND...

 

<?php
// Bitwise AND 0101 (from last code section) to 0001, if it is not all bits 0, then the setting was enabled
if($userSettings & SET_1 != 0) { 
    // User has this setting
}
?>

 

If you don't know the bitwise operators, then this all probably made no sense to you and you should look it up in the manual: http://us.php.net/language.operators.bitwise (no worries though, I've never used them for anything beyond permissions systems)

Hey Hey Hey... I figured it out!!!! =]  Your amazing. Pointed me in the right Direction... It was alot simpler than I had originally Thought. Basically this is a very generic concept of what I did... I don't think its exactly like how your mentioned but It works.

 

<?php
include 'config.php';
$uid = "2";
$profilefields = mysql_fetch_assoc(mysql_query("SELECT * FROM *******_vbulletin.userfield WHERE userid = '$uid'", $dbh)) or die("Could not Select something or another darnit...");
$usersetting = $profilefields[field5];
$cod4 = 1;
$domo = 2;
$wow = 4;
$gw = 8;
$halo = 16;
$others = 32;
$box1 = $usersetting & $cod4;
if ($box1 != 0) {
echo "I play Call of Duty 4 </br>";
}
$box2 = $usersetting & $domo4;
if ($box2 != 0) {
echo "I play Dream of Mirror Online </br>";
}
$box3 = $usersetting & $wow;
if ($box3 != 0) {
echo "I play World of Warcraft </br>";
}
$box4 = $usersetting & $gw;
if ($box4 != 0) {
echo "I play Guild Wars </br>";
}
$box5 = $usersetting & $halo;
if ($box5 != 0) {
echo "I play Halo</br>";
}
$box6 = $usersetting & $others;
if ($box6 != 0) {
echo "I play Other Games </br>";
}
echo "End.";

?> 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.