PDA

View Full Version : Multiple-Selection Checkbox Decoding


brandondrury
09-26-2007, 02:48 PM
Yesterday, I needed to decode a Multiple-Selection Checkbox from vBulletin's custom profile for an external script. I'm not really that experienced at programming, but I through this together and it has worked for me quite well.

I figured most people wouldn't be all that interested in dealing with "binary stuff". I know I wasn't.

// Change 'field11' to match the field that corresponds to your multi-selection form box
$field = $vbulletin->userinfo['field11'];

// Change the values of this array to correspond IN ORDER with the values you use in your vBulletin custom profile field
$cat= array ('Society', 'Science','Recreation','News','Health','Government ','Entertainment','Education','Computers','Busines s','Art');
$counter = count($cat);

$watch = explode(" ", chunk_split(str_pad(decbin($field), $counter, "-", STR_PAD_LEFT),1," "));

$j=0;
for ($i=0; $i<$counter; $i++)
{
if ($watch[$i]==1)
{
$w[$j]=$cat[$i]; $j++;
}
}
// This will spit out the values the user selected
print_r($w);

If you find this script useful, send 100 billion dollars to Brandon Drury. :D

Opserty
09-26-2007, 04:32 PM
Can you give an example value of $vbulletin->userinfo['field1'] and an example output? :p

brandondrury
09-26-2007, 06:22 PM
Sure

$vbulletin->userinfo['field11'] value would be = 1058

The output (if I print the array) would be with my example categories/values:

Array ( [0] => Society [1] => Government [2] => Business )

----

$vbulletin->userinfo['field11'] value would be = 1

The output (if I print the array) would be with my example categories/values:

Array ( [0] => Art )

Brandon

Marco van Herwaarden
09-27-2007, 08:28 AM
Hmm isn't this stored as a bitfield?

So:

1 = Society
2 = Government
4!! = Business
8 = Next option
etc..

For example a value of 9 would mean that both "Society" (1) and "Next option" (8) is selected.

Use bitwise operators to see if an option is selected.

To test if "Government" (2) is selected:
if ($vbulletin->userinfo['field11'] & 2)

brandondrury
09-28-2007, 12:10 AM
What's a bitfield?

I guess I'll have to add that to me "to do" list.

The method I used seams to work for what I'm doing, but I can almost guarantee there is a better way. I couldn't find anything by searching on vBulletin.org or in Google, so maybe this is a start.

I'm sure it's a fairly rare situation anyway.

Brandon

Dismounted
09-28-2007, 05:10 AM
Bitfields are used all across vBulletin, and they're pretty useful. As Marco has demonstrated, each successful "bit" is double the previous. There is a maximum of bits, of course, but that is something like 2^32.

Paul M
09-28-2007, 06:39 PM
Just to be clear - the maximum number of bits is normally 32, the value of the 32nd bit would be 2^31.