title: Codeigniter Get Nested Hierarchical Data from Database
body: <p>How can i get Hierarchical Data from db in Codeigniter. I read this : 
  <a href="http://www.sitepoint.com/hierarchical-data-database/" rel="nofollow">http://www.sitepoint.com/hierarchical-data-database/</a>
  And i do good that but i cant optimize this tutorial with my model, controler and views</p>
  
  <pre><code> Default Category
     |----- Sub category
            | ----One more category
                 |----- Somthing else  
  </code></pre>
  
  <p>I try but dont show sub category:</p>
  
  <p>My model:</p>
  
  <pre><code>   public function fetchChildren($parent, $level) {    
         $this-&gt;handler = $this-&gt;db-&gt;query("SELECT * FROM content_categories WHERE parent_id='".$parent."' ");
            foreach($this-&gt;handler-&gt;result() as  $row ) {
                $this-&gt;data[$row-&gt;id] = $row;
                //echo str_repeat('  ',$level).$row['title']."\n"; 
            }
  
            return $this-&gt;data;
  </code></pre>
  
  <p>}</p>
  
  <p>Controller :</p>
  
  <pre><code>  $this-&gt;data['node'] = $this-&gt;categories_model-&gt;fetchChildren(' ',0);
  </code></pre>
  
  <p>Views:</p>
  
  <pre><code>&lt;table class="module_table"&gt;
      &lt;thead&gt;
          &lt;tr&gt;
               &lt;th&gt;&lt;?php echo lang('categories_table_title'); ?&gt;&lt;/th&gt;     
          &lt;/tr&gt;
      &lt;/thead&gt;
  
      &lt;tbody&gt;
          &lt;?php foreach ($node as $row) : ?&gt;
          &lt;tr&gt;
              &lt;th&gt; &lt;?php echo str_repeat('|----', 0+1). $row-&gt;title ?&gt; &lt;/th&gt;
          &lt;/tr&gt;
          &lt;?php endforeach; ?&gt;
      &lt;/tbody&gt;
  
  &lt;/table&gt;
  </code></pre>
  
  <p>And output is :</p>
  
  <pre><code>----Default
  ----Default
  ----Test Category 1
  ----Seccond Test Category 1
  ----Another Test Category 1 
  </code></pre>
  
  <p>When i do this in model all work fine but when i try that to call in controler and loop in view i have result like above example:</p>
  
  <p>This work onlu in model:</p>
  
  <pre><code>   public function fetchChildren($parent, $level) {    
         $this-&gt;handler = $this-&gt;db-&gt;query("SELECT * FROM content_categories WHERE parent_id='".$parent."' ");
            foreach($this-&gt;handler-&gt;result() as  $row ) {
               echo str_repeat('|-----',$level).$row-&gt;title."\n"; 
              $this-&gt;fetchChildren($row-&gt;title, $level+1);
            }
  
            return $this-&gt;data;
  </code></pre>
  
  <p>}</p>
  
  <p>And like output i have :</p>
  
  <pre><code>Default
      |----Test Category 1
      |----Seccond Test Category 1
          |----Another Test Category 1 
  </code></pre>
  
  <p>Any one have solution or example thanks.</p>