title: Get all categories (multilevel)
body: <p>I'm using codeigniter and have a table with 3 columns (id, name, parent_id). A category can have many subcategories and a subcategory can have many sub-sub categories.</p>
  
  <p>I've been trying to get all categories and their subcategories using this code:</p>
  
  <pre><code>public function getCategory($id)
  {
          $categories = array();
          while($id != 0)
          {
                  $this-&gt;db-&gt;from('categories'); //$this-&gt;table is a field with the table of categoris
                  $this-&gt;db-&gt;where('id', $id);
                  $this-&gt;db-&gt;limit(1);
                  $result = $this-&gt;db-&gt;get()-&gt;row(); //fetching the result
                  $categories[] = $result;
                  $id = $result-&gt;parent_id;
          }
          return $categories;
  }
  
     public function getAllCategories()
      {
              $this-&gt;db-&gt;select('id');
              $this-&gt;db-&gt;from('categories'); //$this-&gt;table is a field with the table of categoris
              $this-&gt;db-&gt;where('parent_id', 0);
              $mainCategories = $this-&gt;db-&gt;get()-&gt;result(); //fetching the result
              $result = array();
              foreach($mainCategories as $id)
              {
                      $result[] = $this-&gt;getCategory($id-&gt;id);
              }
              return $result;
      }
  </code></pre>
  
  <p>But it returns me only 1 level categories.</p>
  
  <p>My question is how to accomplish my task: get all categories and subcategories for every level.</p>