Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: Atomixx on January 05, 2019, 03:47:55 PM

Title: Displaying user's primary membergoup on a page
Post by: Atomixx on January 05, 2019, 03:47:55 PM
Hello!

I am working with a form on my forum and I want some simple details to be autocompleted beforehand. These details are the date, forum username and the user's primary member group. I've been able to accomplish the date and username using $context['current_time'] and  $context['user']['name'] respectively. However, I've had trouble displaying the member group as it seems $context['member']['name'] does not work. I have not been able to find a way to define $context['member']. Is there an easier way I would go about this?
Title: Re: Displaying user's primary membergoup on a page
Post by: vbgamer45 on January 05, 2019, 03:50:20 PM
Check $context['member']['group']


$groupName = (!empty($context['member']['group']) ? $context['member']['group'] : $context['member']['post_group']);
Title: Re: Displaying user's primary membergoup on a page
Post by: Arantor on January 05, 2019, 04:02:33 PM
That only works if $context['member'] is defined - which it frequently won't be (and isn't the case the OP mentions)

I think you can make it be defined with loadMemberData($user_info['id']); $context['member'] = loadMemberContext($user_info['id']); but my memory could be faulty.
Title: Re: Displaying user's primary membergoup on a page
Post by: Atomixx on January 05, 2019, 04:21:34 PM
Quote from: Arantor on January 05, 2019, 04:02:33 PM
That only works if $context['member'] is defined - which it frequently won't be (and isn't the case the OP mentions)

I think you can make it be defined with loadMemberData($user_info['id']); $context['member'] = loadMemberContext($user_info['id']); but my memory could be faulty.

Yes defining $context['member'] is my problem as I see $context['member']['name'] does not work aswell. Still can't seem to make it work even with the code you supplied me. Looks like $context['member'] is defined in View-Profile.php as

$context['member'] = &$memberContext[$memID];

Edit: Doesnt seem to work either. Any ideas?
Title: Re: Displaying user's primary membergoup on a page
Post by: xTyler on January 05, 2019, 05:23:26 PM
Which page is your form going to be on? Is it an action page or are you trying to use it outside of smf?
Title: Re: Displaying user's primary membergoup on a page
Post by: Atomixx on January 05, 2019, 05:28:44 PM
Quote from: xTyler on January 05, 2019, 05:23:26 PM
Which page is your form going to be on? Is it an action page or are you trying to use it outside of smf?

I'm using Simple portal to create pages. This form will be on that page. So yes its on the forums
Title: Re: Displaying user's primary membergoup on a page
Post by: xTyler on January 05, 2019, 05:38:27 PM
The way I've done it on my forum is not the quickest and im sure there is a better route but I call $user_settings['id_group'] and then depending on the id work out the membergroup.
Title: Re: Displaying user's primary membergoup on a page
Post by: Arantor on January 05, 2019, 08:33:41 PM
Quote from: Atomixx on January 05, 2019, 04:21:34 PM
Quote from: Arantor on January 05, 2019, 04:02:33 PM
That only works if $context['member'] is defined - which it frequently won't be (and isn't the case the OP mentions)

I think you can make it be defined with loadMemberData($user_info['id']); $context['member'] = loadMemberContext($user_info['id']); but my memory could be faulty.

Yes defining $context['member'] is my problem as I see $context['member']['name'] does not work aswell. Still can't seem to make it work even with the code you supplied me. Looks like $context['member'] is defined in View-Profile.php as

$context['member'] = &$memberContext[$memID];

Edit: Doesnt seem to work either. Any ideas?


You need both the statements I had, not just the second one.
Title: Re: Displaying user's primary membergoup on a page
Post by: Atomixx on January 06, 2019, 09:36:22 PM
Quote from: Arantor on January 05, 2019, 08:33:41 PM
Quote from: Atomixx on January 05, 2019, 04:21:34 PM
Quote from: Arantor on January 05, 2019, 04:02:33 PM
That only works if $context['member'] is defined - which it frequently won't be (and isn't the case the OP mentions)

I think you can make it be defined with loadMemberData($user_info['id']); $context['member'] = loadMemberContext($user_info['id']); but my memory could be faulty.

Yes defining $context['member'] is my problem as I see $context['member']['name'] does not work aswell. Still can't seem to make it work even with the code you supplied me. Looks like $context['member'] is defined in View-Profile.php as

$context['member'] = &$memberContext[$memID];

Edit: Doesnt seem to work either. Any ideas?


You need both the statements I had, not just the second one.


Yes I used both. My code is below



global $context, $txt, $user_info, $user_profile;

loadMemberData($user_info['id']);
$context['member'] = loadMemberContext($user_info['id']);

echo '

<a>', $context['member']['group'], '</a>';
Title: Re: Displaying user's primary membergoup on a page
Post by: Atomixx on January 12, 2019, 10:50:46 AM
Bump?  :'(
Title: Re: Displaying user's primary membergoup on a page
Post by: Arantor on January 12, 2019, 12:26:47 PM
What if you instead do var_dump($context['member']); instead of your echo? It won't show the correct output but it will show what ended up in the $context variable.