PDA

View Full Version : Display custom field in memberlist that contains multiple selections.


AFemaleProdigy
03-24-2011, 05:13 AM
Yep, it's me again. ;)

I have been using the code below to display custom fields in my new custom memberlist. It's been working great so far, but I just realized it doesn't work like I intended when the custom field values are from a multiple selection check box.

{vb:raw userinfo.field17}

So, if check box values are any combination of A,B,C,D - then that code outputs a number value according to the number value of boxes the user checked. How do I get it to output the actual textual value of the boxes checked by the user?

Example:

User can select any number of the following - Snacks, Breakfast, Lunch, Dinner

So if they check Breakfast and Dinner, that is what I want the output to be in my template. Instead, the code above will output the number assocciated with that option.

Thanks! :D

OcR Envy
03-24-2011, 04:05 PM
Custom Fields with more than one option are sorted based on the geometric formula. Meaning You would do something like this:

Userfield6(Radio, Select, Checkbox it doesn't matter)
Yes(1)
No(2)
Maybe(4)
Never(8)

if($userfield['field6'] & 1)
$ans = 'Yes';
elseif($userfield['field6'] & 2)
$ans = 'No';
elseif($userfield['field6'] & 4)
$ans = 'Maybe';
elseif($userfield['field6'] & 8)
$ans = 'Never';This continues on each time you multiply the number by itself
so to continue it would be 1,2,4,8,16,32,64 etc..

AFemaleProdigy
03-24-2011, 05:18 PM
I checked the values in the databse to make sure I had them correct, but this doesn't seem to work. My template is still outputting numbers instead of text values.

if($userfield['field23'] & 1)
$ans = 'Unknown';
elseif($userfield['field23'] & 2)
$ans = 'No Food';
elseif($userfield['field23'] & 8)
$ans = 'Breakfast';
elseif($userfield['field23'] & 24)
$ans = 'Lunch';
elseif($userfield['field23'] & 32)
$ans = 'Dinner';
elseif($userfield['field23'] & 63)
$ans = 'Snacks';

{vb:raw userinfo.field23}

OcR Envy
03-24-2011, 05:58 PM
I didn't convert it to template format

Something like this:

<vb:if condition="$userfield['field23'] & 1">
Yes
<vb:elseif condition="userfield['field23'] & 2">
No
<vb:endif>

AFemaleProdigy
11-27-2011, 09:45 PM
I am using this in my template and am getting an error when I try to save the template. Can anyone see a problem with this?

<vb:if condition="$userfield['field23'] & 1">
Unknown
<vb:elseif condition="userfield['field23'] & 2" />
No Food
<vb:elseif condition="userfield['field23'] & 24" />
Breakfast
<vb:elseif condition="userfield['field23'] & 32" />
Lunch
<vb:elseif condition="userfield['field23'] & 63" />
Dinner
<vb:elseif condition="userfield['field23'] & 8" />
Snacks
</vb:if>Here is the error message:

The following error occurred when attempting to evaluate this template:
%1$s
This is likely caused by a malformed conditional statement. It is highly recommended that you fix this error before continuing, but you may continue as-is if you wish.

kh99
11-28-2011, 02:57 AM
All your condition expressions except the first one are missing the $ in front of "userfield".

AFemaleProdigy
11-28-2011, 03:35 AM
I can't believe I didn't see that!! Lol!

However, I was finally able to save the template changes, but that code is not outputting anything. Grrr! This is what I have...

<vb:if condition="$userfield['field23'] & 1">
Unknown
<vb:elseif condition="$userfield['field23'] & 2" />
No Food
<vb:elseif condition="$userfield['field23'] & 24" />
Breakfast
<vb:elseif condition="$userfield['field23'] & 32" />
Lunch
<vb:elseif condition="$userfield['field23'] & 63" />
Dinner
<vb:elseif condition="$userfield['field23'] & 8" />
Snacks
</vb:if>Also, is using elseif appropriate if there may be multiple options set? For example: Breakfast, Lunch, Dinner

kh99
11-28-2011, 03:42 AM
It might help to temporarily output the value of $userfield['field23'] to see what's going on.

That seems like a strange set of bits to check (normally they'd be 1, 2, 4, 8, 16, 32, 64, etc), but I don't know exactly how they're being used so what you have could be correct.

AFemaleProdigy
11-28-2011, 03:51 AM
I got those numbers from the database. They may have skipped around from me deleting and adding options. How can I temporarily output the values?

kh99
11-28-2011, 03:55 AM
I got those number from the database.
OK, but the 63 seems strange since 63 in binary is 111111, so that condition will be true if any of those bits are set. That would mean that the last "Snacks" condition would never be displayed. (Maybe you just want "==" instead of "&"?)

How can I temporarily output the values?

I think you could just put {vb:raw userfield.field23} somewhere in the template.

AFemaleProdigy
11-28-2011, 04:17 AM
Okay, you were right about the numbers. Some were wrong. I updated the numbers though and it still didn't do anything. I tried using == like you said and that didn't work. Did I do it like you were thinking... ?

<vb:if condition="$userfield['field23'] == 1">
Unknown
<vb:elseif condition="$userfield['field23'] == 2" />
No Food
<vb:elseif condition="$userfield['field23'] == 4" />
Breakfast
<vb:elseif condition="$userfield['field23'] == 8" />
Lunch
<vb:elseif condition="$userfield['field23'] == 16" />
Dinner
<vb:elseif condition="$userfield['field23'] == 32" />
Snacks
</vb:if>

kh99
11-28-2011, 05:25 AM
OK, I went back and read the above posts again, and since it's multiple selection, I think you do want to use '&'. And are you sure $userfield is what you want to check? You might want $bbuserinfo[field23] (assuming this is displaying the data to the user who selected it, and not to another user (like an admin or someone).

Also, like you mentioned in an earlier post, since it's possible to have multiple options selected you don't want to use elseif, you would just want a bunch of if statements, like:


<vb:if condition="$bbuserinfo['field23'] & 1">Unknown </vb:if>
<vb:if condition="$bbuserinfo['field23'] & 2">No Food </vb:if>
<vb:if condition="$bbuserinfo['field23'] & 4">Breakfast </vb:if>


etc.

AFemaleProdigy
11-28-2011, 09:18 PM
This output will be shown to any usergroup type viewing it on a public profile and also in the custom member list I created. It is not meant to show the viewer their own personal selections, but to show the viewer the selection of whoever's profile they are viewing.

kh99
11-28-2011, 09:25 PM
In that case you don't want to use $bbuserinfo, of course.

AFemaleProdigy
11-28-2011, 09:54 PM
So this does not work either. Any ideas?

<vb:if condition="$userfield['field23'] & 1">
Unknown
<vb:elseif condition="$userfield['field23'] & 2" />
No Food
<vb:elseif condition="$userfield['field23'] & 4" />
Breakfast
<vb:elseif condition="$userfield['field23'] & 8" />
Lunch
<vb:elseif condition="$userfield['field23'] & 16" />
Dinner
<vb:elseif condition="$userfield['field23'] & 32" />
Snacks
</vb:if>

kh99
11-28-2011, 10:06 PM
Should have asked this from the start, but which template are you putting that in?

AFemaleProdigy
11-29-2011, 02:51 AM
It is going in the template memberinfo_block_aboutme

kh99
11-29-2011, 03:09 AM
Then I think you want this:

<vb:if condition="$userinfo['field23'] & 1">Unknown </vb:if>
<vb:if condition="$userinfo['field23'] & 2">No Food </vb:if>
<vb:if condition="$userinfo['field23'] & 4">Breakfast </vb:if>
etc

AFemaleProdigy
11-29-2011, 12:54 PM
When I try to save the template with that, I get an error that says "adding child to non-existent node!".

<vb:if condition="$userinfo['field23'] & 1">
Unknown</vb:if>
<vb:if condition="$userinfo['field23'] & 2" />
No Food</vb:if>
<vb:if condition="$userinfo['field23'] & 4" />
Breakfast</vb:if>
<vb:if condition="$userinfo['field23'] & 8" />
Lunch</vb:if>
<vb:if condition="$userinfo['field23'] & 16" />
Dinner</vb:if>
<vb:if condition="$userinfo['field23'] & 32" />
Snacks</vb:if>

kh99
11-29-2011, 01:05 PM
I think you need to take the '/'s out of the <vb:if... tags, like this:

<vb:if condition="$userinfo['field23'] & 1">
Unknown</vb:if>
<vb:if condition="$userinfo['field23'] & 2" >
No Food</vb:if>
<vb:if condition="$userinfo['field23'] & 4" >
Breakfast</vb:if>
<vb:if condition="$userinfo['field23'] & 8" >
Lunch</vb:if>
<vb:if condition="$userinfo['field23'] & 16" >
Dinner</vb:if>
<vb:if condition="$userinfo['field23'] & 32" >
Snacks</vb:if>

AFemaleProdigy
11-29-2011, 01:12 PM
I had just now figured that out and came back to tell you. Lol! BUT... IT ALL WORKS NOW!! Yeay!!!!

Thanks sooooo much for your help!! I really appreciate it! :D

For anyone that wants to know, the final working code to display a custom user profile field variable (multiple selection checkbox values) in a template is:

<vb:if condition="$userinfo['field23'] & 1">
Unknown</vb:if>
<vb:if condition="$userinfo['field23'] & 2">
No Food</vb:if>
<vb:if condition="$userinfo['field23'] & 4">
Breakfast</vb:if>
<vb:if condition="$userinfo['field23'] & 8">
Lunch</vb:if>
<vb:if condition="$userinfo['field23'] & 16">
Dinner</vb:if>
<vb:if condition="$userinfo['field23'] & 32">
Snacks</vb:if>

Edit the numbers accordingly, of course. ;)

TheLastSuperman
11-29-2011, 07:26 PM
Edit: Nevermind I just saw your post after refreshing :cool:.

What template are you including this in?

*You can view the current usergroups in the Usergroup Manager, hover over the usefield link to see a URL displayed in your browser and note the id# ;).

Simon Lloyd
11-29-2011, 07:39 PM
I don't know much but shouldn't it be something like this:<vb:if condition="$vbulletin->userinfo['field23'] == '1'">

kh99
11-29-2011, 07:52 PM
I don't know much but shouldn't it be something like this:<vb:if condition="$vbulletin->userinfo['field23'] == '1'">

It's a multiple selection profile field, so you can think of the value as a binary number where each possible selection is represented by one of the bits. The '&' operator can be used to test one bit at a time.

AFemaleProdigy
12-09-2011, 02:06 PM
What I posted last does work. I am going to attempt to put this together as a mod. I will reference it here when I am done. Wish me luck! I've never posted a mod before!

@KH99 and @OcR Envy, I will include you for helping me! :)