Show the user which user group he belongs to

joit

Active Member
Hi,

I'd like to make visible for the user which user group he belongs to. I saw there's the table user_usergroup_map which has the relevant data but I have no idea how to display that in a nice way because there's just the IDs.

Any thoughts?
 
Sorry I missed replying to this question....

I've not tried but I think this will do:

PHP:
$groups = JUserHelper::getUserGroups($userid);
Will return an array of user group ids.

If you to get the group names in an array:
PHP:
$groups = JUserHelper::getUserGroups($userid);
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('title')->from('#__usergroups')->where('id IN (' . implode(',', $groups) . ')');
$db->setQuery($query);
$groupsLabels = $db->loadColumn();
 
Kind of depends where you want to show the information, which you didn't specify.

Best place would probably be in a custom template. Or if you don't want to overhead of maintaining a custom template, you could run it using something like Direct PHP or Jumi in your form / list intro or outro, which might be the better bet.

You'll also need to output the information, with something like ...

PHP:
echo "<p>You are a member of:</p><ul>"
foreach ($groupsLabels as $groupLabel) {
    echo "<li>$groupLabel</li>"
}
echo "</ul";

-- hugh
 
It seams there's a little error in the foreach statement. I pasted this:
PHP:
<?php
 
$groups = JUserHelper::getUserGroups($userid);
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('title')->from('#__usergroups')->where('id IN (' . implode(',', $groups) . ')');
$db->setQuery($query);
$groupsLabels = $db->loadColumn();
 
echo "<p>You are a member of:</p><ul>"
foreach ($groupsLabels as $groupLabel) {
    echo "<li>$groupLabel</li>"
}
echo "</ul";
 
?>

And received:
You are a member of:
Warning: Invalid argument supplied for foreach() in /mnt/web1/e0/43/5190843/htdocs/test/bl/plugins/content/DirectPHP/DirectPHP.php(58) : eval()'d code on line 11
 
that means your query is not right.
My first thought is that would cause an sql error if $groups was empty. So add a check for that:

PHP:
<?php
 
$groups = JUserHelper::getUserGroups($userid);

if (empty($groups)) {
  return;
}
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('title')->from('#__usergroups')->where('id IN (' . implode(',', $groups) . ')');
$db->setQuery($query);
$groupsLabels = $db->loadColumn();
 
echo "<p>You are a member of:</p><ul>"
foreach ($groupsLabels as $groupLabel) {
    echo "<li>$groupLabel</li>"
}
echo "</ul>";
 
?>
if that doesn't help you'd need to examine the database error:

PHP:
<?php
 
$groups = JUserHelper::getUserGroups($userid);

if (empty($groups)) {
  return;
}
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('title')->from('#__usergroups')->where('id IN (' . implode(',', $groups) . ')');
$db->setQuery($query);
$groupsLabels = $db->loadColumn();

echo $db->getErrorMsg();
echo "<p>You are a member of:</p><ul>"
foreach ($groupsLabels as $groupLabel) {
    echo "<li>$groupLabel</li>"
}
echo "</ul>";
 
?>
 
I checked both codes (with inserting 2 semicolongs at the foreach statement) but like this there is nothing echo'd at all!
Any ideas?
 
can you paste what code you have now please?

At a(nother) guess, I would think that you don't have a value assigned to $userid. Which I should have provided when I first gave you the example :oops:

So perhaps:

PHP:
<?php
$user = JFactory::getUser();
$groups = JUserHelper::getUserGroups($user->get('id'));
 
if (empty($groups)) {
  echo "no groups!!!!";
  return;
}
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('title')->from('#__usergroups')->where('id IN (' . implode(',', $groups) . ')');
$db->setQuery($query);
$groupsLabels = $db->loadColumn();
 
echo $db->getErrorMsg();
echo "<p>You are a member of:</p><ul>"
foreach ($groupsLabels as $groupLabel) {
    echo "<li>$groupLabel</li>";
}
echo "</ul>";
 
?>
 
ok,
with this code:
PHP:
<?php
$user = JFactory::getUser();
$groups = JUserHelper::getUserGroups($user->get('id'));
 
if (empty($groups)) {
  echo "no groups!!!!";
  return;
}
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('title')->from('#__usergroups')->where('id IN (' . implode(',', $groups) . ')');
$db->setQuery($query);
$groupsLabels = $db->loadColumn();
 
echo $db->getErrorMsg();
echo "<p>You are a member of:</p><ul>";
foreach ($groupsLabels as $groupLabel) {
    echo "<li>$groupLabel</li>";
}
echo "</ul>";
 
?>

I receive "no groups!!!!" if not logged in and the name of the group if I'm logged in.
Thanks a bunch guys!
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top