title: MySQL/PHP/HTML How to insert children in hierarchical dropdown form menu?
body: <p>I have the following variables that fetch values from my database. These variables are storing Parent values of a hierarchical list (formats) and children values of the same hierarchical list:</p>
  
  <pre><code>$getSolos = $wpdb-&gt;get_results($wpdb-&gt;prepare("
          SELECT * FROM wp_terms p 
          LEFT OUTER JOIN wp_term_taxonomy t ON p.term_id = t.term_id
          WHERE t.taxonomy = 'format'
          AND t.parent = 0
          AND t.term_id NOT IN (SELECT parent FROM wp_term_taxonomy WHERE taxonomy = 'format' AND parent &gt; 0)
          ORDER BY t.parent
          "));
  
  $getParents = $wpdb-&gt;get_results($wpdb-&gt;prepare("
          SELECT * FROM wp_terms p 
          LEFT OUTER JOIN wp_term_taxonomy t ON p.term_id = t.term_id
          WHERE t.taxonomy = 'format'
          AND t.parent = 0
          AND t.term_id IN (SELECT parent FROM wp_term_taxonomy WHERE taxonomy = 'format' AND parent &gt; 0)
          "));
  </code></pre>
  
  <p>Although the above isn't necessarily important, the following is as my database looks like this:</p>
  
  <pre><code>term_id name          slug                   term_id1    parent
  1         Entry Form    entry-form           1           0
  2         Facebook      facebook             2           0
  3         Entry Form    entry-form-facebook  3           2
  4         Twitter       twitter              4           0
  5         Page          page-facebook        5           2
  </code></pre>
  
  <p>So logically, my OBJECT ARRAYS contain:</p>
  
  <pre><code>$getParents-&gt;parent = 2 // contains only IDs of parent formats
  $getSolos-&gt;term_id = 1,5 // contains only IDs of formats that have no children
  </code></pre>
  
  <p>The idea is to populate a dropdown form menu with these formats, with parent formats being optgroups containing their children. SO far I've achieved the following:</p>
  
  <pre><code>&lt;form&gt;
      Format: &lt;select name="format"&gt;
          &lt;option value="empty"&gt;&lt;/option&gt;
              &lt;?php 
                  foreach ($getSolos as $solo) {
                      echo "&lt;option value='".$solo-&gt;name."' ".$selected."&gt;".$indent.$solo-&gt;name."&lt;/option&gt;";
                  }
                  foreach ($getParents as $parent) {
                      echo "&lt;optgroup label='".$parent-&gt;name."'&gt;";
                      // How do I get the children in here?
                      echo "&lt;/optgroup&gt;";
                  }
              ?&gt;
      &lt;/select&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>The above outputs:</p>
  
  <pre><code>Entry Form
  Twitter
  Facebook // Bold as it is an optgroup header
  </code></pre>
  
  <p>The output should be:</p>
  
  <pre><code>Entry Form
  Twitter
  Facebook // Bold as it is an optgroup header
      Entry Form
      Page
  </code></pre>
  
  <p>So here is my question:
  1) How do I get the children to appear inside the parent optgroup?</p>