Display group name?

Permalink
Hi guys,

Just wondered if anyone could help me out, i'm trying to display the 'group name' of a logged in member. I've achieved displaying their member name and a few other attributes using the following code:

$u = new User();
$ui = UserInfo::getByID($u->getUserID());
print $ui->getUserName();


I've tried replacing the getUserName with 'getGroupMemberType()' and 'getGroupMemberName()' but having no luck, and they were just guesses. Am I calling the wrong attributes or can groups not be called in the same way?

Thanks for any help!

 
12345j replied on at Permalink Reply
12345j
since a user can be in multiple groups you have to specify what group object you want to check.
OliR replied on at Permalink Reply
Thanks, does that have something to do with:

$g = Group::getByName('groupname');


In my scenario, I will be adding users manually, and assigning each one to either group a, b, c, or d (for example), so I only need it to return one value. Is it possible to return that single value? Not too sure what group object I will need to declare since they will all be in individual groups... (I'm sure i'll be making more of a meal of this than is needed)!

Still getting used to C5 so you'll have to bare with me at the mo :)
12345j replied on at Permalink Reply
12345j
really rough example
$u=new User();
$groups = $u->getUserGroups();
foreach($groups as $group){
echo $group;
}
if there is only one group then that should display the name.
OliR replied on at Permalink Reply
Thanks, that's now showing the group the user belongs too but also 'registered users' and 'guest' on either side of it. Is there any way of excluding them so they don't show up as well? Appreciate the help!
12345j replied on at Permalink Best Answer Reply
12345j
so the order is like
array(
[Registered Users groupID]=>Registered Users 
[Group b groupID]=>Group b 
[GuestgroupID]=>Guest
)
? There are several possibilities, try something like this.
$u=new User();
$groups=$u->getUserGroups();
foreach($groups as $key=>$group){
if($key!==1&&$key!==2){
echo $group;
}
}
OliR replied on at Permalink Reply
Brilliant, that sorted it! Thanks again for your help :-)