TransWikia.com

Display specific Joomla user groups

Joomla Asked by niaziblog on September 5, 2021

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

One Answer

A basic query to to select parent and its children from a nested table, taken from JoomlaCMSTableNested::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;
        ...

Answered by Sharky on September 5, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP