0

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";
    $db->setQuery($qry);

    $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

1 Answer 1

1

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)
    ->select('n.*')
    ->from('#__usergroups AS n, #__usergroups AS p')
    ->where('n.lft BETWEEN p.lft AND p.rgt')
    ->where('p.id = ' . (int) $groupId)
    ->order('n.lft');

$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;
        ...

Your Answer

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

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