title: PHP mysql Tree Child count and List All Child node level wise
body: <p>I have a tree like <br/>
  <a href="https://i.stack.imgur.com/sQF4q.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sQF4q.png" alt="Tree Diagram" /></a>
  <br/>and I want the output for requeted ID for example Admin <br/>
  <a href="https://i.stack.imgur.com/CHX0p.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CHX0p.png" alt="required output." /></a></p>
  <p><br/>my table structure is <br/>
  <a href="https://i.stack.imgur.com/jS1ci.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jS1ci.png" alt="Table/Database Structure" /></a></p>
  <p>I have a method that returns Child count level wise but I also want to return the child list level-wise with child count level-wise like 2nd image output required.</p>
  <pre><code>function childCountLevelWise($conn,$ID, $level){
  if ($level&gt;14){
      $count = array(0=&gt;0); 
      return $count;
  }
  $sql=&quot;select * from user_my_tree t where t.parent_ID=&quot;.$ID;
  $result=returnResults($conn,$sql);
  if ($result==null){
      $count = array(0=&gt;0); 
  }
  else{
      $count = array(0=&gt;0);
      foreach($result as $key=&gt;$row)
      {   
          $count[0]++;
          $children=childCountLevelWise($conn,$row['ID'], $level+1);
          $index=1;
          foreach ($children as $child)
          {
              if ($child==0)
                  continue;
              if (isset($count[$index]))
                  $count[$index] += $child;
              else    
                  $count[$index] = $child;
                  $index++;
          }    
      }    
  }
  return $count; 
  </code></pre>
  <p>}</p>