How to get list of users of defined group?

Hi Forum. I need your help.

I need to get users of defined group.

that’s how I get the group.

$group = $modx->getObject('modUserGroup', array ('id' => '2')); // I get the group
$users = $group->getMany('UserGroupMembers'); // I get the users of this group

Now I want to check if I get the group I need. count($users); It prints 4. 4 users inside the group. that’s right.

Now I want to show these users on the page.

foreach ($users as $user){
$userdata = $user->toArray();
$profile = $user->getOne('Profile');
$profileData = $profile->toArray();
// username/id
$name = $user->get('username');
$uid = $user->get('id');

$isUserBlocked = $profile->get('blocked');
$extendedFields = $profile->get('extended');

$age = $extendedFields['age'];

// print
if(!preg_match("/(.*?)admin(.*)/iu", $name)){
if($isUserBlocked != '1'){
echo '
<div class="usersforu boxs">
<div class="ufuName">'.$name.'</div>
<div class="ufuAge">Age: '.$age.' '.$unitsAge.'</div>
......
</div>
';
}
}



//end for each
}

I’ve got the problem. There is no users shown on the page. foreach prints nothing; I guess I should disassemble the array $user with another way. Could tell me how to?

With the line

$users = $group->getMany('UserGroupMembers');

you get back objects of the class modUserGroupMember and NOT modUser.

Try

$user_group_member->getOne('User');

to access the related modUser.

I’m sorry, what is $user_group_member?

Sorry for the confusion. Try this:

$user_group_members = $group->getMany('UserGroupMembers');
foreach ($user_group_members as $user_group_member){
	$user = $user_group_member->getOne('User');
	//add the rest of your code here
}

Thanks a lot. Wish you good luck.

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.