News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

accessing/reading custom profile field in index.template.php

Started by T_o_m, March 02, 2011, 12:57:38 PM

Previous topic - Next topic

T_o_m

Using SMF 2.0 RC4

I have some global functionality that my users can customize (choose various options) as a profile field (account settings).

Thanks to custom fields being built-in, having the select dropdown added to the profile was quick and easy.

But how do I read/access this from the index.template.php file (since it's a global function, I think that is the most appropriate place to put it) ?

I've tried various globals ($context, $user_info, etc.) and I've tried loading it via loadMemberData(), loadMemberContext() and both - and I just can't get it loaded. Worse, I'm out of ideas where and how, someone here having an idea is my last hope before I proceed to simply plug a database query into the code.

texasman1979

#1
Somplace appropriate before the template call a fuction designed by you to pull that info from db and store nec stuff in context,then you have stuff available in template. If it is something needed every page load, you can include and call ur function from near start of smf main in index.php. I can give example of this if need when i get home fron work this evening. Someone else may can tell you how to access ur custom field thru loadusersettings, but i cant do that either at moment.
SMF 2.0.4
SimplePortal 2.3.5

LOGIC is a FOUR letter word! :)


live627

Quote from: T_o_m on March 02, 2011, 12:57:38 PMand I've tried loading it via loadMemberData(), loadMemberContext()
Veery close - the latter function has a second param which will tell it to load the fields  - have a look at Load.php for more details

T_o_m

Quote from: live627 on March 03, 2011, 01:14:40 AM
Quote from: T_o_m on March 02, 2011, 12:57:38 PMand I've tried loading it via loadMemberData(), loadMemberContext()
Veery close - the latter function has a second param which will tell it to load the fields  - have a look at Load.php for more details

I have. It didn't work for me. The best I could get was that it had an array custom_fields - which was empty. And yes, my custom field is set to active.

Does anyone have a working code example? I'm much better with code than with explanations.

T_o_m

My current code that fails:

loadMemberData(array($context['user']['id']), false, 'profile');
loadMemberContext($context['user']['id'], true);
global $memberContext;
var_dump($memberContext);


$memberContext contains an element "custom_fields" - which is empty. My custom field is active and I can see and change it in my profile.

Why is this so complicated? You'd think that people who activate custom fields actually want to use them for something. :-(

Arantor

Because it's just not designed for that. Quite why you'd want to show the user something on every page that they themselves entered is slightly confusing to me.

If you unserialize $modSettings['displayFields'], what do you get, out of interest?

texasman1979

i dont think that it is so much displaying something they put in as it is processing something different off of it.
SMF 2.0.4
SimplePortal 2.3.5

LOGIC is a FOUR letter word! :)


T_o_m

Quote from: Arantor on March 03, 2011, 07:28:55 AM
Because it's just not designed for that. Quite why you'd want to show the user something on every page that they themselves entered is slightly confusing to me.

If you unserialize $modSettings['displayFields'], what do you get, out of interest?

I'm not trying to show them something.

My custom field is a selection box for various options that they can choose. Depending on what they pick there, I want them to show various things. Think "optional content boxes".

Thanks to the hints here, and lots and lots of code reading and even more experiments, I've finally found a version that works:

$id = $context['user']['id'];
loadMemberData(array($id));
global $user_profile;

after that, $user_profile[$id]['options'][(custom field name)] contains the data I need.

brad_irc

Quote from: T_o_m on March 03, 2011, 10:25:42 AM
Thanks to the hints here, and lots and lots of code reading and even more experiments, I've finally found a version that works:

$id = $context['user']['id'];
loadMemberData(array($id));
global $user_profile;

after that, $user_profile[$id]['options'][(custom field name)] contains the data I need.


Sorry to bump this oldish topic, but thank you for the correct solution. Was having the exact same issue :)

Advertisement: