title: Nested sub category right under parent category in codeigniter
body: <p>I want to to display the list of category in a table and the sub category of each parent category right under.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I want it look like this:</p>
  
  <blockquote>
    <p>Category One</p>
    
    <p>-> Sub category One #1</p>
    
    <p>-> Sub category One #2</p>
    
    <p>Category Two</p>
    
    <p>-> Sub category Two #1</p>
    
    <p>-> Sub category Two #2</p>
    
    <p>Category Three</p>
    
    <p>-> Sub category Three #1</p>
    
    <p>-> Sub category Three #2</p>
  </blockquote>
  
  <p>But what I got is:</p>
  
  <blockquote>
    <p>Category One </p>
    
    <p>Category Two </p>
    
    <p>Category Three</p>
    
    <p>-> Sub category One #1</p>
    
    <p>-> Sub category One #2</p>
    
    <p>-> Sub category Two #1 ... etc</p>
  </blockquote>
  
  <p>Here is the model:</p>
  
  <pre><code>function get_categories() {
      $data = array();
      $this-&gt;db-&gt;group_by('parent_id,category_id');
      $query = $this-&gt;db-&gt;get('category');
      if ($query-&gt;num_rows() &gt; 0) {
          foreach ($query-&gt;result() as $row) {
              $data[] = $row;
          }
      }
      return $data;
  }
  </code></pre>
  
  <p>And in the controller I call this function with <code>$data['categories']=$this-&gt;category_model-&gt;get_categories()</code>. And I display the data in the view with this code:</p>
  
  <pre><code>&lt;?php foreach ($categories as $cat) : ?&gt;
     &lt;tr&gt;
        &lt;td&gt;&lt;?php echo $cat-&gt;name ?&gt;&lt;/td&gt;
        &lt;td&gt;&lt;?php echo $cat-&gt;description ?&gt;&lt;/td&gt;
     &lt;/tr&gt;
  &lt;?php endforeach; ?&gt;
  </code></pre>
  
  <p>But the problem is, instead of under each their parent category, the sub categories are all showing on the bottom of the list. Please help me to fix this problem. Thanks.</p>