Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 09-26-2007, 02:48 PM
brandondrury brandondrury is offline
 
Join Date: Oct 2005
Posts: 136
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default 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.
Reply With Quote
  #2  
Old 09-26-2007, 04:32 PM
Opserty Opserty is offline
 
Join Date: Apr 2007
Posts: 4,103
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Can you give an example value of $vbulletin->userinfo['field1'] and an example output?
Reply With Quote
  #3  
Old 09-26-2007, 06:22 PM
brandondrury brandondrury is offline
 
Join Date: Oct 2005
Posts: 136
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
Reply With Quote
  #4  
Old 09-27-2007, 08:28 AM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
Reply With Quote
  #5  
Old 09-28-2007, 12:10 AM
brandondrury brandondrury is offline
 
Join Date: Oct 2005
Posts: 136
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
Reply With Quote
  #6  
Old 09-28-2007, 05:10 AM
Dismounted's Avatar
Dismounted Dismounted is offline
 
Join Date: Jun 2005
Location: Melbourne, Australia
Posts: 15,047
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #7  
Old 09-28-2007, 06:39 PM
Paul M's Avatar
Paul M Paul M is offline
 
Join Date: Sep 2004
Location: Nottingham, UK
Posts: 23,748
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Just to be clear - the maximum number of bits is normally 32, the value of the 32nd bit would be 2^31.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 10:15 PM.


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.03659 seconds
  • Memory Usage 2,226KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (2)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (7)post_thanks_box
  • (7)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (7)post_thanks_postbit_info
  • (7)postbit
  • (7)postbit_onlinestatus
  • (7)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.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/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.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
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete