0

I'm very new to Joomla and PHP, however I'm attempting to echo the logged in users assigned groups (group id 58-62, 72). A user can only be in one of those groups at any point and time. Below is the code I'm using. It doesn't display any errors, and echos the first line, but no echo based off groupid. Also, the print_r doesn't return any data.

Any guidance on what I'm certain is a silly mistake on my part would be appreciated.

$groups = $user->get('groups');
print_r($groups);
if($groups) {
    echo "Echo user Group Name : ";
    if (isset($groups[62])) echo " - Group 62 <Br/>";
    if (isset($groups[61])) echo " - Group 61 text <Br/>";
    if (isset($groups[60])) echo " - Group 60 text <Br/>";
}else{
    echo 'Not logged in<Br/>';
}
}
2
  • The groupId's are correct. I pulled them from the SQL table and checked them again. Oct 30, 2015 at 6:15
  • 1
    Well, you have changed now the code in your question - this makes parts of my answer below irrelevant and may be confusing for other users if they ever arrive here with a similar issue.
    – FFrewin
    Oct 30, 2015 at 10:13

1 Answer 1

4

I guess you first have created the user object.

$user = JFactory::getUser();

Then the $user->group is an array, not an object, and it will contain all the usergroups a user belongs. Therefore the $group->id is wrong (enable error reporting so you can see any errors).

Do a print_r($user->group), so you will be able to see the full array and its key/values, and decide what you want to do later with your logic.

Keep in mind that all users (even guests) will belong to a user-group, so a groups will always exist.

You could check against your desired usergroups with something like:

if (in_array(62, $groups)) {// do something here}

Or you can always loop through an array :

<?php foreach ($groups as $group) : ?>
 // do something
<?php endforeach; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.