title: How to get all child of a user group in Joomla?
body: <p>I have a <code>user_groups</code> table</p>
  
  <pre><code>id  parent_id       title
  1       0           Public
  2       1           Registered
  3       2           Author
  4       3           Editor
  12      2           Customer Group (Example)
  </code></pre>
  
  <p>You can see here Register(2) have 2 child id(3,12) and id(3) also have it's child(4). So it's create a tree. Now I need to make such tree in list box. So I have tried like this:</p>
  
  <pre><code>&lt;?php
  $db = JFactory::getDbo(); 
  $query = "select grp.id, grp.title from #__usergroups AS grp where parent_id=2";
  $db-&gt;setQuery($query);  
  $groups = $db-&gt;loadObjectList();
  ?&gt;
  &lt;tr&gt;
      &lt;td&gt;Select Group&lt;/td&gt;
      &lt;td&gt;
      &lt;select name="usergroup"&gt;
      &lt;option value="0"&gt;&lt;?='Select Group';?&gt;&lt;/option&gt;                 
      &lt;?
      foreach($groups as  $group) {
  
          $query = "select grp.id, grp.title from #__usergroups AS grp where parent_id=".$group-&gt;id;
          $db-&gt;setQuery($query);  
          $groups2 = $db-&gt;loadObjectList();
      ?&gt;
      &lt;option value="&lt;?=$group-&gt;id?&gt;"&gt;&lt;?=$group-&gt;title?&gt;&lt;/option&gt;
      &lt;?  if($groups2!=''){
              foreach($groups2 as $group2){
                  $query = "select grp.id, grp.title from #__usergroups AS grp where parent_id=".$group2-&gt;id;
                  $db-&gt;setQuery($query);  
                  $groups3 = $db-&gt;loadObjectList();
              ?&gt;
              &lt;option value="&lt;?=$group2-&gt;id?&gt;"&gt;--&lt;?=$group2-&gt;title?&gt;&lt;/option&gt;
              &lt;?
                  if($groups3!=''){
                      foreach($groups3 as $group3){
                          $query = "select grp.id, grp.title from #__usergroups AS grp where parent_id=".$group3-&gt;id;
                          $db-&gt;setQuery($query);  
                          $groups4 = $db-&gt;loadObjectList();
                          ?&gt;
                          &lt;option value="&lt;?=$group3-&gt;id?&gt;"&gt;----&lt;?=$group3-&gt;title?&gt;&lt;/option&gt;
                          &lt;?
                          if($groups4!=''){
                              foreach($groups4 as $group4){
                                  ?&gt;
                                  &lt;option value="&lt;?=$group4-&gt;id?&gt;"&gt;--------&lt;?=$group4-&gt;title?&gt;&lt;/option&gt;
                                  &lt;?
                              }
                          }
                      }
                  }
              }
          }
      }?&gt;
      &lt;/select&gt;
      &lt;/td&gt;
  &lt;/tr&gt;
  </code></pre>
  
  <p>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!</p>