Back to vBulletin 3 Articles

[How-to] Decode custom user profile bitfield
by joelabq 05 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”.

Code:
	$custom_field_qry = $db->query_read("SELECT data from ". TABLE_PREFIX."profilefield where profilefieldid = '7' ");
	$custom_field_ary = $db->fetch_array($custom_field_qry);
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:

Code:
	$my_custom_data = unserialize($custom_field_ary['data']);
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):
Code:
foreach($my_custom_data AS $key => $val) 
{ 
  if ($vbulletin->userinfo[field7] & pow(2,$key)) 
  { 
       $show[testbit] .= $val." ";
  }
}

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

Code:
$stylevar[htmldoctype]
<html dir="$stylevar[textdirection]" lang="$stylevar[languagecode]">
<head>
	<!-- no cache headers -->
	<meta http-equiv="Pragma" content="no-cache" />
	<meta http-equiv="Expires" content="-1" />
	<meta http-equiv="Cache-Control" content="no-cache" />
	<!-- end no cache headers -->
	$headinclude
	<title><phrase 1="$vboptions[bbtitle]">$vbphrase[x_powered_by_vbulletin]</phrase></title>
</head>
<body>
$header
$navbar
Testing: $show[testbit]
<!-- main -->
Now go into the plugin manage, and add a plugin at “forumhome_start”, copy this code (as explained above):

Code:
$custom_field_qry = $db->query_read("SELECT data from ". TABLE_PREFIX."profilefield where profilefieldid = '7' ");
$custom_field_ary = $db->fetch_array($custom_field_qry);
$my_custom_data = unserialize($custom_field_ary['data']);
foreach($my_custom_data AS $key => $val) 
{ 
  if ($vbulletin->userinfo[field7] & pow(2,$key)) 
  { 
       $show[testbit] .= $val." ";
  }
}
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.

vblts.ru supports vBulletin®, 2022-2024