Register Members List Search Today's Posts Mark Forums Read

Reply
 
Article Options
[How-to] Decode custom user profile bitfield
joelabq
Join Date: Oct 2005
Posts: 15

by joelabq joelabq is offline 04 Jan 2006

This How to _should_ help someone setup custom profile fields that use multiple selections decode the bitfields for display (or other) purposes.

If you see anything wrong with how I did this, please let me know, likewise if you have suggestions on how to improve it. I'm just putting this out there because no one else has, and I've had a few people ask about it.

Let’s assume you are using Field7, and you have three settings you defined as multi-select checkbox (Yes, No, and Maybe). You can add more fields if you wish to test more.

1.Run a query to extract the data from the database for that profile field… These are all the settings for this field as defined in your custom profile field manager in the AdminCP. The following code will extract the settings you defined…in this case, “Yes”, “No”, and “Maybe”.


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

2. If you are using any of the following field types in the custom field manager – “select”, “radio”, “checkbox”, “select_multiple” the data is stored serialized, see php.net’s manual for serialize and unserialize to understand the meaning of the function if you do not already understand it. In this case, we used “multi-select checkbox”, so we unserialize the data:


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

This will load the array structure stored in the “data” field of the database into $my_custom_data

3. Now we need to match the values from $my_custom_data to the values in $custom_user_data. The “odd” thing here is we are matching binary data in custom_user_data, represented in a decimal number, to the real field in $my_custom_data, and set the values in $show[testbit] so we can display them in a template like forumhome. You may choose how you want to do this, of coarse (refer to the operator reference to understand how “&” works at http://us2.php.net/manual/en/language.operators.php):

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.


So for our example, edit the forumhome template, and add “Testing: $show[testbit]” right below $navbar like shown below:


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

Now go into the plugin manage, and add a plugin at “forumhome_start”, copy this code (as explained above):


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

Make sure the plugin is activated, and save it.

Now load up your UserCP, check mark whatever boxes you like, and then load up your main forumhome homepage. It should show you all the options you select (and only those options) delimited by a space.
Views: 2770
Reply With Quote
Comments
  #2  
Old 04 Jan 2006, 22:34
Antivirus's Avatar
Antivirus Antivirus is offline
 
Join Date: Sep 2004
Joel, that works really nicely for FORUMHOME, but I am trying to get it to work in the MEMBERINFO template and using hook location member_complete and it's returning my profile's data, not the member who's profile i am viewing...

Thanks!
__________________
Please feel free to PM me about custom work, installations, and upgrades!

Eclipse Records - Streetcult Street Team

Last edited by Antivirus; 04 Jan 2006 at 22:53.
Reply With Quote
  #3  
Old 04 Jan 2006, 23:22
joelabq joelabq is offline
 
Join Date: Oct 2005
Originally Posted by Antivirus
Joel, that works really nicely for FORUMHOME, but I am trying to get it to work in the MEMBERINFO template and using hook location member_complete and it's returning my profile's data, not the member who's profile i am viewing...

Thanks!
The line that has this:

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

Is what you need to modify. The var $vbulletin->userinfo[field7] is only the current logged in user's information.

What you need to do is query for that users profile fieldX values.

In memberinfo, only hidden field aren't already decoded... So I'm not sure what you want to do exactly, but you could make sure they are not hidden and vB already will display the correct values for that user in their profile.

If you really need this in member info, you could add a query using a plugin at member_customfields, something to this effect:


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

Then pull that data out in a variable, and put that in place of the code above where $vbulletin->userinfo[field7] is used... Like so:


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

Hope that helps.

Joel
Reply With Quote
  #4  
Old 05 Jan 2006, 17:31
Antivirus's Avatar
Antivirus Antivirus is offline
 
Join Date: Sep 2004
It took me a couple hours to figure it out due to process of elimination and with your help above as well- but the following works for me:

Create a plugin using hook location member_complete with following code:


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

and include $show[testbit] in the template...

BUT...

It does however add 1 additional query to the page. I don't understand why it needs to run another query at all, since the correct serialized data is already in $userinfo[field26] to begin with, but it does now work at the least.

I was attempting to unserialize the data and match it up without having to run the additional query above. I tried the following, but I am still a beginner with php and it wasn't working properly.


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

__________________
Please feel free to PM me about custom work, installations, and upgrades!

Eclipse Records - Streetcult Street Team
Reply With Quote
  #5  
Old 05 Jan 2006, 19:48
vietkieu_cz vietkieu_cz is offline
 
Join Date: Dec 2005
Some screenshot please?
Reply With Quote
  #6  
Old 05 Jan 2006, 19:49
joelabq joelabq is offline
 
Join Date: Oct 2005
You have to run the query because field26 in the user info does not contain the actual data, just the bitfield data. All you get in the bitfield is 1's and 0's. Basically you have to match the real array with $userinfo[field26].

To visualize may be easier... imagine your array with "Yes", "No" and "Maybe" values (in that order in your custom field manager), which is contained NOT in userinfo[$field26] but in $my_custom_data as outlined above. $my_custom_data looks like this:

$my_custom_data[0] = "Yes"
$my_custom_data[1] = "No"
$my_custom_data[2] = "Maybe"

Now, $userinfo[field26] looks like this when "Yes" is set (in binary):
001 (or simply "1", but the leading zero's may help you visualize here)

So when you compare the fields with " if ($userinfo[field26] & pow(2,$key)" you are essentially doing this:

$my_custom_data[0] = "Yes" 1 (YES, user has it)
$my_custom_data[1] = "No" 0 (No, User doesnt have it set)
$my_custom_data[2] = "Maybe" 0 (No, User doesnt have it set)

Now, imagine Yes and Maybe are set for this user. $userinfo[field26] will be set to "5" in decimal, and when converted to binary is "101"... ON, OFF, ON... so lets compare it to $my_custom_data:

$my_custom_data[0] = "Yes" 1 (YES, user has it)
$my_custom_data[1] = "No" 0 (No, User doesnt have it set)
$my_custom_data[2] = "Maybe" 1 (YES, user has it)

Likewise, the code you posted:


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

...Will not work, because the data that is in $userinfo[field26] is NOT the serialized data, it only hold the binary representation to what is set when compared to the real serialized data. Therefore, you MUST run a query, unless vB already has pulled the data.

In your case, around line 560 in member.php


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

...Which means, you could use the already queried $userinfo["$profilefieldname"], but ONLY if it's not hidden. I take it your data is hidden, therefore you could either unhide and create some template conditionals, or you add this query.

Hope this helps.

Joel

Originally Posted by vietkieu_cz
Some screenshot please?
What?

Last edited by joelabq; 05 Jan 2006 at 20:36. Reason: Spelling errors.
Reply With Quote
  #7  
Old 05 Jan 2006, 20:17
Antivirus's Avatar
Antivirus Antivirus is offline
 
Join Date: Sep 2004
that helps me understand the bitfields more, thanks Joel, you have been a tremendous help!
__________________
Please feel free to PM me about custom work, installations, and upgrades!

Eclipse Records - Streetcult Street Team
Reply With Quote
  #8  
Old 05 Jan 2006, 21:28
joelabq joelabq is offline
 
Join Date: Oct 2005
Glad I could help.

Joel
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Article Options

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


New To Site? Need Help?

All times are GMT. The time now is 08:00.

Layout Options | Width: Wide Color: