简体   繁体   English

如何在Joomla中获得用户组的所有孩子?

[英]How to get all child of a user group in Joomla?

I have a user_groups table 我有一个user_groups

id  parent_id       title
1       0           Public
2       1           Registered
3       2           Author
4       3           Editor
12      2           Customer Group (Example)

You can see here Register(2) have 2 child id(3,12) and id(3) also have it's child(4). 你可以在这里看到注册(2)有2个孩子的身份证(3,12)和身份证(3)也有它的孩子(4)。 So it's create a tree. 所以它创造了一棵树。 Now I need to make such tree in list box. 现在我需要在列表框中创建这样的树。 So I have tried like this: 所以我试过这样的:

<?php
$db = JFactory::getDbo(); 
$query = "select grp.id, grp.title from #__usergroups AS grp where parent_id=2";
$db->setQuery($query);  
$groups = $db->loadObjectList();
?>
<tr>
    <td>Select Group</td>
    <td>
    <select name="usergroup">
    <option value="0"><?='Select Group';?></option>                 
    <?
    foreach($groups as  $group) {

        $query = "select grp.id, grp.title from #__usergroups AS grp where parent_id=".$group->id;
        $db->setQuery($query);  
        $groups2 = $db->loadObjectList();
    ?>
    <option value="<?=$group->id?>"><?=$group->title?></option>
    <?  if($groups2!=''){
            foreach($groups2 as $group2){
                $query = "select grp.id, grp.title from #__usergroups AS grp where parent_id=".$group2->id;
                $db->setQuery($query);  
                $groups3 = $db->loadObjectList();
            ?>
            <option value="<?=$group2->id?>">--<?=$group2->title?></option>
            <?
                if($groups3!=''){
                    foreach($groups3 as $group3){
                        $query = "select grp.id, grp.title from #__usergroups AS grp where parent_id=".$group3->id;
                        $db->setQuery($query);  
                        $groups4 = $db->loadObjectList();
                        ?>
                        <option value="<?=$group3->id?>">----<?=$group3->title?></option>
                        <?
                        if($groups4!=''){
                            foreach($groups4 as $group4){
                                ?>
                                <option value="<?=$group4->id?>">--------<?=$group4->title?></option>
                                <?
                            }
                        }
                    }
                }
            }
        }
    }?>
    </select>
    </td>
</tr>

But this is not the best way to it. 但这不是最好的方法。 If I add more child item then this will not work untill I make another level. 如果我添加更多的子项目,那么这将无法工作,直到我进入另一个级别。 So I need the effective way to do it so I don't need to add level in the future. 所以我需要有效的方法来做到这一点,所以我不需要在将来添加级别。 Please help! 请帮忙!

You can use optgroup for this like, 您可以使用optgroup ,例如,

<select>
  <optgroup label="Group 1">
    <option>Option 1.1</option>
  </optgroup> 
  <optgroup label="Group 2">
    <option>Option 2.1</option>
    <option>Option 2.2</option>
  </optgroup>
  <optgroup label="Group 3" disabled>
    <option>Option 3.1</option>
    <option>Option 3.2</option>
    <option>Option 3.3</option>
  </optgroup>
</select>

Read this https://developer.mozilla.org/en-US/docs/Web/HTML/Element/optgroup 阅读此https://developer.mozilla.org/en-US/docs/Web/HTML/Element/optgroup

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM