vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Multiple-Selection Checkbox Decoding (https://vborg.vbsupport.ru/showthread.php?t=158815)

brandondrury 09-26-2007 02:48 PM

Multiple-Selection Checkbox Decoding
 
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.

PHP Code:

// 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','Business','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:
PHP Code:

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.


All times are GMT. The time now is 09:36 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01019 seconds
  • Memory Usage 1,730KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (2)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (7)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete