title: codeigniter tree view build data
body: <p><a href="https://i.stack.imgur.com/oVyPg.png" rel="nofollow noreferrer">This is the db structure</a></p>
  
  <pre><code>[
    {
      text: 'Parent 1',
      href: '#parent1',
      tags: ['4'],
      nodes: [
        {
          text: 'Child 1',
          href: '#child1',
          tags: ['2'],
          nodes: [
            {
              text: 'Grandchild 1',
              href: '#grandchild1',
              tags: ['0']
            },
            {
              text: 'Grandchild 2',
              href: '#grandchild2',
              tags: ['0']
            }
          ]
        },
        {
          text: 'Child 2',
          href: '#child2',
          tags: ['0']
        }
      ]
    },
    {
      text: 'Parent 2',
      href: '#parent2',
      tags: ['0']
    },
    {
      text: 'Parent 3',
      href: '#parent3',
       tags: ['0']
    }
  ]
  </code></pre>
  
  <p>The code i used is this,</p>
  
  <pre><code>public function getCategoryTree($level = 0, $prefix = '') {
          $rows = $this-&gt;db
              -&gt;select('id,parent_id,name')
              -&gt;where('parent_id', $level)
              -&gt;order_by('id','asc')
              -&gt;get('category')
              -&gt;result();
  
          $json_response = array();
          foreach ($rows as $row)
          {
              $row_array = array();
              $row_array['text'] = $row-&gt;name;        
              $row_array['nodes'] = array();
              $newlevel = $row-&gt;id;  
  
          $childs = $this-&gt;db
              -&gt;select('id,parent_id,name')
              -&gt;where('parent_id', $newlevel)
              -&gt;order_by('id','asc')
              -&gt;get('category')
              -&gt;result();
  
              if(count($childs) &gt; 0){ 
                  foreach ($childs as $row)
                  {
                      $row_array['nodes'][] = array(
                          'text' =&gt; $row-&gt;name,
                      );
                  }
              }
              array_push($json_response, $row_array); //push the values in the array
          }
          return $json_response;
  
      }
  </code></pre>
  
  <p>The output i got:
  <a href="https://i.stack.imgur.com/bNYYK.png" rel="nofollow noreferrer">output</a></p>
  
  <p>The above code gives only one level, but I want to go deep. How do I format the loops to get it done?</p>