Check and/or multiple profile options are filled?

Started by samborabora, July 16, 2014, 06:00:34 PM

Previous topic - Next topic

samborabora

I need to check if the gender selection is chosen (not left blank) and/or one of the custom fields has been filled in. This doesn't work:

if (!empty($context['member']['gender']['name']) || !empty($context['custom_fields']))
echo '
f00
';


Where did I go wrong?

margarett

Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

Sir Osis of Liver

You can set a custom field as 'require entry', so user must enter something to register.  Deactivate the Gender field, and replace it with a required custom field.
Ashes and diamonds, foe and friend,
 we were all equal in the end.

                                     - R. Waters

samborabora

Sorry, I always stupidly forget to mention which page it's wanted on! It's profile.template.php, so a users profile.

Sir Osis of Liver

You have the option to make custom fields visible in user profile in the field settings.  You have to activate the feature in Admin -> Core Features -> Advanced Profile Fields, then you'll see it in Admin -> Features and Options -> Profile Fields.
Ashes and diamonds, foe and friend,
 we were all equal in the end.

                                     - R. Waters

samborabora

Thanks, yes that's ture, but I just need to check IF any of the fields are filled in, just a simple query to see IF any of the fields and/or a gender has been picked from the gender selection box. I think the gender check is:

if (!empty($context['member']['gender']['name'])

But I can't quite figure out how to check IF any of the custom fields have anything other than a null value.

Sir Osis of Liver

I think you'd have to check each field individually, with the correct index, same way you're trying to check the gender field.
 
Ashes and diamonds, foe and friend,
 we were all equal in the end.

                                     - R. Waters

Kindred

custom fields are not as simple - I don't think the get loaded into context
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

Sir Osis of Liver

Then you'd have to do a db query to check the fields.
Ashes and diamonds, foe and friend,
 we were all equal in the end.

                                     - R. Waters

samborabora

I'm trying to check if ANY of the custom fields have a value, not one specific field, why is !empty($context['custom_fields']) not quite right?

Kindred

because.... to the best of my knowledge, NONE of the custom field are ever loaded into the context array
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

Illori

from Display.template.php

// Any custom fields to show as icons?
if (!empty($message['member']['custom_fields']))
{
$shown = false;
foreach ($message['member']['custom_fields'] as $custom)
{
if ($custom['placement'] != 1 || empty($custom['value']))
continue;
if (empty($shown))
{
$shown = true;
echo '
<li class="im_icons">
<ul>';
}
echo '
<li>', $custom['value'], '</li>';
}
if ($shown)
echo '
</ul>
</li>';
}

Kindred

yeah... but that is ONLY loaded in DIsplay -- and is loaded for the display of the stuff in the message....   it's loaded by poster for each message and is never added to $context for the current user
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

Arantor

Except in the profile area, the fields relevant to a given page are loaded into $context['custom_fields'] as already shown.

Checking if multiple are set is a proper pain in the arse though because it specifically requires iterating over $context['custom_fields'] and checking each one specifically. It would help if the OP actually provided sufficient detail in this case such as which field(s) need to be present as well as gender to output the relevant text.

samborabora

Quote from: ‽ on July 17, 2014, 08:00:55 AM
Except in the profile area, the fields relevant to a given page are loaded into $context['custom_fields'] as already shown.

Checking if multiple are set is a proper pain in the arse though because it specifically requires iterating over $context['custom_fields'] and checking each one specifically. It would help if the OP actually provided sufficient detail in this case such as which field(s) need to be present as well as gender to output the relevant text.

No fields NEED to be present, I just need to check if they have any value at all. If "gender" is anything but blank (not male or female selected) AND/OR any of my custom fields "facebook", "twitter", "soundcloud" or "last.fm" have any value in them, I want to show a bounding "hide/show" wrapper around this output. If they are all blank, I won't show a wrapper, and therefore, no show/hide.

margarett

Are you talking about profile modify (where one introduces profile information) or profile summary?

If it's the first, using custom profile fields and let SMF deal with the automatic error/empty messages is probably easier and better.
If it's the second, and you are working on a "template tweak", then you have to iterate $context['custom_fields'] as explained.
Quote from: ‽ on July 17, 2014, 08:00:55 AM
Checking if multiple are set is a proper pain in the arse though because it specifically requires iterating over $context['custom_fields'] and checking each one specifically.
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

samborabora

#16
Quote from: ‽ on July 17, 2014, 08:00:55 AM
Checking if multiple are set is a proper pain in the arse though because it specifically requires iterating over $context['custom_fields'] and checking each one specifically.

Sorry, yes it's profile summary, I've been trying to iterate the custom fields (and I assume I'd have to run a check for all 4 in addition to the gender dropdown) but I'm having trouble getting the code right. What would I use to query if all 4 fields and the dropdown are selected? Custom field names are "face_pro", cust_last", cust_soundc" and "cust_twitte".

margarett

Before the content get's echo'd you need to run a foreach on $context['custom_fields']. The establish some logic inside the foreach.

I also don't know the custom fields structure, but a piece of

echo '<pre>';
print_r($context['custom_fields']);
echo '</pre>';

Will allow you to show the array structure and the correct way to pick up whatever values you wish to analyze. ;)
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

samborabora

Quote from: margarett on July 17, 2014, 10:39:24 AM
Before the content get's echo'd you need to run a foreach on $context['custom_fields']. The establish some logic inside the foreach.

I also don't know the custom fields structure, but a piece of

echo '<pre>';
print_r($context['custom_fields']);
echo '</pre>';

Will allow you to show the array structure and the correct way to pick up whatever values you wish to analyze. ;)

Well, this would probably be somewhat like what that might print out:
foreach ($context['custom_fields'] as $field)
if($field['colname'] == "cust_aboutm0"){
echo ' <p class="profeaboutme">';

But I'm not entirely sure how to CHECK if there's a value, or what way to phrase this exactly to get it to give the result I'm looking for. I'm still not quite 'with' the lingo of the custom fields, since it all seems to be done via arrays?

margarett

Probably something like
if(($field['colname'] == 'cust_aboutm0') && !empty($field['value']))
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

Advertisement: