This code snippet below shows the list of all Joomla user groups(registered,editor.superuser and ...) at component

enter image description here

    // load in joomla user groups
    $qry = "SELECT * FROM #__usergroups ORDER BY lft";

    $this->groups = $db->loadObjectList();

    $group_index = array();

    foreach ($this->groups as &$group)
        $group_index[$group->id] = &$group;

        if ($group->parent_id == 0)
            $group->level = 0;  
        } else {
            $group->level = $group_index[$group->parent_id]->level + 1;

        $group->display = str_repeat("- ", $group->level) . $group->title;

    array_unshift($this->groups, JHTML::_('select.option', '', JText::_("JOOMLA_USERGROUP"), 'id', 'display'));
    $this->jgroup_select = JHTML::_('select.genericlist',  $this->groups, 'usergroup', 'class="inputbox" size="1" onchange="document.fssForm.submit( );"', 'id', 'display', $this->usergroup);

now I want to show only registered user and manager user list or only registered user


A basic query to to select parent and its children from a nested table, taken from Joomla\CMS\Table\Nested::getTree():

$query = $db->getQuery(true)
    ->from('#__usergroups AS n, #__usergroups AS p')
    ->where('n.lft BETWEEN p.lft AND p.rgt')
    ->where('p.id = ' . (int) $groupId)

$this->groups = $db->setQuery($query)->loadObjectList();

Where $groupId is the topmost group's ID.

You also need to modify your code here:

if ($group->parent_id == 0)

By adding a check for array element with topmost group's ID :

if (!isset($group_index[$group->parent_id]) || $group->parent_id == 0)

This is because the parent of your topmost group is not retrieved. Alternatively, add a counter to the loop and check for first run:

foreach ($this->groups as $key => &$group)
    if ($key === 0)
        $group->level = 0;
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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