title: How to looping hierarcial data with parent id in Codeigniter
body: <p>How to create multilevel foreach and showing hierarcialy,
  I have a table like this </p>
  
  <pre><code>+----+------------------+-----------+
  | id | name             | parent_id |
  +----+------------------+-----------+
  | 1  | parent1          | 0         |
  +----+------------------+-----------+
  | 2  | child1           | 1         |
  +----+------------------+-----------+
  | 3  | parent2          | 0         |
  +----+------------------+-----------+
  | 4  | child2           | 3         |
  +----+------------------+-----------+
  | 5  | grandchild1      | 4         |
  +----+------------------+-----------+
  | 6  | greatgrandchild1 | 5         |
  +----+------------------+-----------+
  </code></pre>
  
  <p>and then I want to showing data like this</p>
  
  <pre><code>&lt;ul&gt;
   &lt;li&gt; parent1
    &lt;ul&gt;
     &lt;li&gt; child1 &lt;/li&gt;
    &lt;/ul&gt;
   &lt;/li&gt;
  &lt;/ul&gt;
  &lt;ul&gt;
   &lt;li&gt; parent2 
    &lt;ul&gt;
     &lt;li&gt; child2 
      &lt;ul&gt;
       &lt;li&gt; grandschild1 
        &lt;ul&gt;
          &lt;li&gt; greatgrandchild1
        &lt;ul&gt;
       &lt;/li&gt;
      &lt;/ul&gt;
     &lt;/li&gt;
    &lt;/ul&gt;
   &lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>and then i create the view</p>
  
  <pre><code>&lt;?php
  $data = $this-&gt;db-&gt;where('parent_id', '0')-&gt;get('mytbl')-&gt;result();
  echo '&lt;ul&gt;'
  foreach ($data as $item)
  {
   echo '&lt;li&gt;'.$item-&gt;name.'&lt;/li&gt;';
  
     echo '&lt;ul&gt;';
  
     $data2 = $this-&gt;db-&gt;where('parent_id',$item_id)-&gt;get('mytbl')-&gt;result();
  
     foreach ($data2 as $item2) {
  
     echo '&lt;li&gt;'.$item2-&gt;name.'&lt;/li&gt;';
  
      echo '&lt;ul&gt;';
  
     $data3 = $this-&gt;db-&gt;where('parent_id',$item_id)-&gt;get('mytbl')-&gt;result();
  
     foreach ($data3 as $item3) {
  
     echo '&lt;li&gt;'.$item3-&gt;name.'&lt;/li&gt;';
  
     // etc ....
  
    }
  
    echo '&lt;/ul&gt;';
  
    }
  
    echo '&lt;/ul&gt;';
  
  
  }
  echo '&lt;ul&gt;';
  ?&gt;
  </code></pre>
  
  <p>and then I think this is not efficient, too many foreach and I just put it into view, Is it possible to create looping like this? without write many of foreach and then how to create this in controller then passing into view,</p>
  
  <p>Anyone know? please help me! Sorry my english is bad.</p>