title: Display categories and subcategories in php mysql
body: <p>Cant get categories and subcategories in php mysql.</p>
  
  <p>Here i get all categories (cats and subcats):</p>
  
  <pre><code>function get_categories() {
      $conn = db_connect();
      $query = "select * from categories";
      $result = $conn-&gt;query($query);
      if(!$result) {
          return false;
      }else {
          $result = db_result_to_array($result);
          return $result;
      }
  }
  </code></pre>
  
  <p>And here i get subcats:</p>
  
  <pre><code>function get_child_cats($parent_id) {
      $conn = db_connect();
  
      $query = "select * from categories where parent_id = '".$parent_id."'";
  
      $result = $conn-&gt;query($query);
  
      if(!$result) {
          return false;
      }else {
          $result = db_result_to_array($result);
          return $result;
      }
  
  }
  </code></pre>
  
  <p>The <code>db_result_to_array()</code> function is here:</p>
  
  <pre><code>    function db_result_to_array($result) {
      $res_array = array();
      for($count = 0; $row = $result-&gt;fetch_assoc(); $count++) {
          $res_array[$count] = $row;
      }
      return $res_array;
  }
  </code></pre>
  
  <p>DB consists of <code>cat_id, cat_name, parent_id</code></p>
  
  <p>Here i am trying to display cats and subcats:</p>
  
  <pre><code>    if(is_array($categories)) {
      echo "&lt;ul&gt;";
      foreach($categories as $row) {
  
          if(!$row['parent_id']) {
              $childs = get_child_cats($parent_id);
              echo "&lt;li&gt;".$row['cat_name'];
              if($row['parent_id']) {
                  echo "&lt;ul&gt;";
                  foreach($childs as $row) {
                      echo "&lt;li&gt;".$row['cat_name']."&lt;/li&gt;";
                  }
                  echo "&lt;/ul&gt;";
              }
              echo "&lt;/li&gt;";
          }
  
      }
      echo "&lt;/ul&gt;";
  }
  </code></pre>
  
  <p>However, i am getting only cats without any subcats. </p>