News:

SMF 2.1.4 has been released! Take it for a spin! Read more.

Main Menu

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

samborabora

#20
Quote from: margarett on July 17, 2014, 12:46:23 PM
Probably something like
if(($field['colname'] == 'cust_aboutm0') && !empty($field['value']))

Okay, trying something like this:
if (!isset($context['disabled_fields']['gender']) && !empty($context['member']['gender']['name']))
if(($field['colname'] == 'face_pro') && !empty($field['value']))
if(($field['colname'] == 'cust_last') && !empty($field['value']))
if(($field['colname'] == 'cust_soundc') && !empty($field['value']))
if(($field['colname'] == 'cust_twitte') && !empty($field['value']))
echo '
WORKS
';


But it doesn't work, how should I tie all these together?

margarett

Untested, can kill your forum, burn your house and drown your cat ;D

I assume you only want to know if either gender or one of the custom fields are filled, right?

If so:
//Control variable for the whole thing
$check = false;
//array with the list of custom fields that we want to check
$list_of_fields = array('face_pro', 'cust_last', 'cust_soundc', 'cust_twitte');

//Check for gender
if (!isset($context['disabled_fields']['gender']) && !empty($context['member']['gender']['name']))
$check = true;

//loop the loop
foreach ($context['custom_fields'] as $field)
{
if (!empty['value'] && in_array($field['colname'], $list_of_fields))
{
   $check = true;
   break;
}
}

//Finally...
if ($check)
  echo 'WORKS';
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, 07:27:44 PM
Untested, can kill your forum, burn your house and drown your cat ;D

I assume you only want to know if either gender or one of the custom fields are filled, right?

If so:
//Control variable for the whole thing
$check = false;
//array with the list of custom fields that we want to check
$list_of_fields = array('face_pro', 'cust_last', 'cust_soundc', 'cust_twitte');

//Check for gender
if (!isset($context['disabled_fields']['gender']) && !empty($context['member']['gender']['name']))
$check = true;

//loop the loop
foreach ($context['custom_fields'] as $field)
{
if (!empty['value'] && in_array($field['colname'], $list_of_fields))
{
   $check = true;
   break;
}
}

//Finally...
if ($check)
  echo 'WORKS';


Trying this, but I'm getting a syntax error on the comma between ['colname'], $list_of_fields What can I substitute it with?

Shambles

Try changing

if (!empty['value'] && in_array($field['colname'], $list_of_fields))

to this

if (!empty($field['value']) && in_array($field['colname'], $list_of_fields))

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

samborabora

Thank you so much, everyone, that works perfectly!! Now to see if the server crashes over time  ;D

samborabora

Here's the finished code for anyone that's interested:

//Control variable for the whole thing
$check = false;
//array with the list of custom fields that we want to check
$list_of_fields = array('face_pro', 'cust_last', 'cust_soundc', 'cust_twitte');

//Check for gender
if (!isset($context['disabled_fields']['gender']) && !empty($context['member']['gender']['name']))
$check = true;

//loop the loop
foreach ($context['custom_fields'] as $field)
{
if (!empty($field['value']) && in_array($field['colname'], $list_of_fields))
{
   $check = true;
   break;
}
}

//Finally...
if ($check)
  echo '
  <input id="hidiv" type="checkbox" value=""/>
  <label id="showlabel" for="hidiv" >More...</label>
  <div class="toshow">
  ';


Then have all of your custom fields and your gender display, and at the end of it, put:

if ($check)
  echo '</div>';


Here's the css, mine doesn't have a hide, just a show but any old show/hide css will do:
#hidiv {
   display:none;
}
#showlabel  {
  display:block;
  font-family: arial, verdana, sans-serif;
  font-size:12px;
  font-weight:bold;
  line-height:34px;
  cursor:pointer;
  clear:both;
}
.toshow ,
#hidiv:checked + label {
  display:none;
}
#hidiv:checked ~ .toshow {
  display:block;
}

I guess it could be modded to add other fields you want to show hide, the hardest part was getting all of the custom fields, the other stuff should be easier.

samborabora

One last thing, I've added the members age to the check, and it works, but unfortunately SMF decides to tell me that Age: n/a if there is no birthday set, which really I can't understand why it would do that when everything else here is as meaningful or meaningless to display as each other. How do i stop it from giving me the N/A back? This is the currently adjusted code I am using:

//Control variable for the whole thing
$check = false;
//array with the list of custom fields that we want to check
$list_of_fields = array('face_pro', 'cust_last', 'cust_soundc', 'cust_twitte');

//Check for age
if (!empty($context['member']['age']))
$check = true;

//Check for gender
if (!isset($context['disabled_fields']['gender']) && !empty($context['member']['gender']['name']))
$check = true;

//loop the loop
foreach ($context['custom_fields'] as $field)
{
if (!empty($field['value']) && in_array($field['colname'], $list_of_fields))
{
   $check = true;
   break;
}
}

//Finally...
if ($check)
  echo '
  <input id="hidiv" type="checkbox" value=""/>
  <label id="showlabel" for="hidiv" >More...</label>
  <div class="toshow">
  ';

if (!empty($context['member']['age']))
echo '
<dt style="margin-top:10px;">', $txt['age'], ':</dt>
<dd style="margin-top:10px;">', $context['member']['age'] . ($context['member']['today_is_birthday'] ? ' &nbsp; <img src="' . $settings['images_url'] . '/cake.png" alt="" />' : ''), '</dd>';


I cut the code after where it is relevant for this particular issue.

samborabora

Is N/A being stored in the mysql table, or is some other part reporting the age as N/A if the birthday value is empty? I really just want it to be a null value like all of the other options if it has nothing set rather than this N/A business.

margarett

Sources/Profile-View.php

// Set the age...
if (empty($context['member']['birth_date']))
{
$context['member'] += array(
'age' => $txt['not_applicable'],
'today_is_birthday' => false
);
}

Just have 'age' => '',
And it becomes empty ;)
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 21, 2014, 06:23:00 AM
Sources/Profile-View.php

// Set the age...
if (empty($context['member']['birth_date']))
{
$context['member'] += array(
'age' => $txt['not_applicable'],
'today_is_birthday' => false
);
}

Just have 'age' => '',
And it becomes empty ;)

I tried that, and it just gave me a 1, lol! I changed it in:

index.english.php:
$txt['not_applicable'] = '';

If I find any missing N/A's around the board after doing this, I'll just make a new txt string, blank it and change the age to refer to that instead, thanks for your help!

Advertisement: