title: Group SQL posts with each other in lists using recursive function
body: <p><strong>Problem:</strong></p>
  
  <p>I am trying to group related posts from SQL with each other using the ul and li-elements. However, I am stuck as I can't get the output correct.</p>
  
  <p><strong>PHP code (1st level):</strong></p>
  
  <pre><code>$query = "SELECT CID, Item AS Name, Parent, Weight FROM betyg_category";
  $result = mysql_query($query) or die ('Error (' . mysql_errno() . ') ' . mysql_error());
  
  $ctree .= '&lt;ul&gt;';
  
  while ($row = mysql_fetch_assoc($result))
  {
      $ctree .= '&lt;li class="category"&gt;'.$row['Name'] . '';
      $ctree .= getLowerRanks($row['CID'], 1, true);
  }
  
  $ctree .= '&lt;/li&gt;&lt;/ul&gt;';
  
  echo $ctree;
  </code></pre>
  
  <p><strong>Recursive function in PHP:</strong></p>
  
  <pre><code>function getLowerRanks( $id, $level, $option = false )
  {   
      if ( $option )
      {
          $response = '';
          $query = "SELECT CID, Item AS Name, Parent, Weight FROM betyg_category WHERE Status = 1 AND Parent= " . $id . " ORDER BY CID ASC";
          $result = mysql_query($query) or die ('Error (' . mysql_errno() . ') ' . mysql_error());
  
          $response .= '&lt;ul&gt;';
  
          while ($row = mysql_fetch_assoc($result))
          {
              for ($i = 0; $i &lt; ($level-1); $i++) $response .= '&amp;nbsp;';
              $response .= '&lt;li class="category"&gt;&amp;nbsp;'.$row['Name'] . '&lt;/li&gt;';
              $response .= getLowerRanks($row['CID'],$level+1, true);
          }
  
          $response .= '&lt;/ul&gt;';
      }
  
      return $response;
  }
  </code></pre>
  
  <p><strong>Desired output:</strong></p>
  
  <pre><code>&lt;ul&gt;
      &lt;li class="category"&gt;Litteratur
          &lt;ul&gt;
              &lt;li class="category"&gt;&amp;nbsp;Integration av källorna&lt;/li&gt;
              &lt;li class="category"&gt;&amp;nbsp;Belysning av egna resultat&lt;/li&gt;
              &lt;li class="category"&gt;&amp;nbsp;Referenser&lt;/li&gt;
          &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li class="category"&gt;Validitet
          &lt;ul&gt;
              &lt;li class="category"&gt;&amp;nbsp;Huvudsyfte&lt;/li&gt;
              &lt;li class="category"&gt;&amp;nbsp;Oberoende och beroende variabler&lt;/li&gt;
              &lt;li class="category"&gt;&amp;nbsp;Analysmetoderna&lt;/li&gt;
          &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li class="category"&gt;Reliabilitet
          &lt;ul&gt;
              &lt;li class="category"&gt;&amp;nbsp;Metodval&lt;/li&gt;
              &lt;li class="category"&gt;&amp;nbsp;Metodbeskrivning&lt;/li&gt;
              &lt;li class="category"&gt;&amp;nbsp;Databearbetning&lt;/li&gt;
          &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li class="category"&gt;Språk, stil och struktur
          &lt;ul&gt;
              &lt;li class="category"&gt;&amp;nbsp;Språk och stil&lt;/li&gt;
              &lt;li class="category"&gt;&amp;nbsp;Struktur&lt;/li&gt;
          &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li class="category"&gt;Arbetssätt
          &lt;ul&gt;
              &lt;li class="category"&gt;&amp;nbsp;Försvar och opposition&lt;/li&gt;
              &lt;li class="category"&gt;&amp;nbsp;Etiska och samhälleliga aspekter&lt;/li&gt;
          &lt;/ul&gt;
      &lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>