News:

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

Main Menu

Get Primary Group Name

Started by ranonman, June 05, 2018, 03:33:36 PM

Previous topic - Next topic

ranonman

It's my understanding that in order to get primary group id and it's this code:


$user_info['groups'][0]


The first element represents primary group id. Is there a way to get primary group name value?

Kindred

you would need to query the database to find the string that matches that group ID
Сл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."

ranonman

That's what I thought. I also thought that there might be other methods or variables that I did not know about. Thank you.

Shambles

In some cases (ie, in some parts of the forum) you should be able to look in $context['groups']

EG, $groupName = $context['groups'][$user_info['groups'][0]]

Been a long while since I checked and it won't work outside of the context where a group name is required.


Wherever you think you need to know the group name, do a print_r($context['groups']);

Kindred

hmmmmm....   I don't see that context variable available anywhere on my site...
Сл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."

Shambles

Yeah it's around, from time to time

ManageBoards.template.php
// List all the membergroups so the user can choose who may access this board.
foreach ($context['groups'] as $group)
echo '
<label for="groups_', $group['id'], '">
<input type="checkbox" name="groups[]" value="', $group['id'], '" id="groups_', $group['id'], '"', $group['checked'] ? ' checked="checked"' : '', ' class="input_check" />
<span', $group['is_post_group'] ? ' class="post_group" title="' . $txt['mboards_groups_post_group'] . '"' : '', $group['id'] == 0 ? ' class="regular_members" title="' . $txt['mboards_groups_regular_members'] . '"' : '', '>
', $group['name'], '


ManageMembergroups.template.php

foreach ($context['groups'] as $group)
echo '
<option value="', $group['id'], '">', $group['name'], '</option>';


Post.template.php
foreach ($context['groups'] as $group)
echo '
<li>
<label for="who_', $group['id'], '"><input type="checkbox" name="who[', $group['id'], ']" id="who_', $group['id'], '" value="', $group['id'], '" checked="checked" class="input_check" /> ', $group['name'], '</label> <em>(', $group['member_count'], ')</em>
</li>';



.. and many others.

Like I said, it won't be available at all times..

ranonman

In my case, I want to be able to check against primary group name on my main website and do stuff based on whoever belongs in that group. I don't think I would be able to access to that variable $context?

Shambles

Not there, no.


Quote from: Kindred on June 05, 2018, 03:50:10 PM
you would need to query the database to find the string that matches that group ID

Kindred

why check against name, though? why not check just against group ID?
Сл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."

Shambles

^ good point, but if you want the name, here's a decent enough method:

Code (php) Select

<?php
$groups 
cache_quick_get('membergroup_list''Subs-Membergroups.php''cache_getMembergroupList', array(),3);

// $groups will contains the list of group names, indexed by numeric value
// In my system:
//   $groups[0] = 'Admin'
//   $groups[1] = 'Global Moderator'

?>



ranonman

Wouldn't I have to use hard coded variable to check against group ID?  Isn't that bad practice? Suppose if primary group is updated in forum and I'd have to update hard coded variable again.

Illori

the id is less likely to change compared to the name.

ranonman

Mmm. All right. I guess I'm over-complicating this logic process.

Thank you for all the feedback and input.

Shambles

But you asked for:

Quote from: ranonman on June 05, 2018, 03:33:36 PM
The first element represents primary group id. Is there a way to get primary group name value?

So, using my example you'd use $groups[$user_info['groups'][0]]

No hard coding there (other than the "0" index)

ranonman

I did try your example and I only got two results: Administrator and Global Moderator. Should be giving more. There are 9 totals.

Advertisement: