title: Get Hierarchical Data from Database
body: <p>Hello i have one stupid question. But maybe is not.
  In database i have table <strong>categories</strong> and i have 4 cols 
  Id, parent_id, title and description.
  Parent_id is child of current category.</p>
  
  <pre><code>Id |  parent_id |  title  |  description
  
  ___________________________________________________
  
  1  |     1      | Main Category | description 
  
  
  2  |     1      | Sub Category  | description 
  
  
  3  |     2      | Other Category | description 
  
  4  |     1      | Some Category | description 
  </code></pre>
  
  <p>Ok i know how to input data and show results; that is not problem. When i show result in table</p>
  
  <pre><code>&lt;table&gt;
     &lt;tr&gt;
          &lt;td&gt; Id &lt;/td&gt;
          &lt;td&gt; Parent &lt;/td&gt;
          &lt;td&gt; Title &lt;/td&gt;
          &lt;td&gt; Desc &lt;/td&gt;
     &lt;/tr&gt;
      &lt;?php foreach($categories as $category): ?&gt;
      &lt;tr&gt;
         &lt;td&gt; &lt;?php echo $category-&gt;id; ?&gt; &lt;/td&gt;
         &lt;td&gt; &lt;?php echo $category-&gt;parent_id; ?&gt; &lt;/td&gt;
         &lt;td&gt; &lt;?php echo $category-&gt;title; ?&gt; &lt;/td&gt;
         &lt;td&gt; &lt;?php echo $category-&gt;eescription; ?&gt; &lt;/td&gt;
     &lt;/tr&gt;
     &lt;?php endforeach;?&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>This works great and shows child categories under under the other parent categories : Table looks like this:</p>
  
  <pre><code>1  |     1      | Main Category | description 
  2  |     1      | Sub Category  | description   
  3  |     2      | Other Category | description    
  4  |     1      | Some Category | description 
  </code></pre>
  
  <p>My problem is how to list all category and sub category like this</p>
  
  <pre><code>    |-- Main Category
         |--- Sub Category 
             |--- One more cat  
  
     |--Other Category   
        |--- Some Category
  </code></pre>
  
  <p>Like this
  <img src="https://i.stack.imgur.com/Ua7UE.png" alt="enter image description here"></p>
  
  <p>Is it possible to fetch this data using forach() ? Any example how to do this.</p>
  
  <p>Thanks</p>