title: Recursive php mysql query/function for tree of categories
body: <p>I have an oscommerce set up and have decided to add a "most popular" product in each category. I need to query all child categories so that I can then query for the most popular product in the list and display it.</p>
  
  <p>HOWEVER.</p>
  
  <p>I have this function and I just can't seem to get the array of category id's out of it. Is this issue to do with scope??</p>
  
  <pre><code> $children_ids = array(); 
  
  function fetch_children($parent) {     
  
   $result = tep_db_query('SELECT `categories_id` FROM categories WHERE parent_id = "'.(int)$parent.'"'); 
  
     while($row = tep_db_fetch_array($result)) {
  
  
          $children_ids[] = (int)$row['categories_id'];
  
  
          fetch_children($row['categories_id']);
     }   
  }
  </code></pre>
  
  <p>At this point I am not trying to access the array of data using the <code>$children_ids</code> variable however this doesn't seem to contain anything!? I've also tried <code>array_push();</code></p>
  
  <p>Any ideas?</p>