problem in counting children category

Posted by moustafa on Stack Overflow See other posts from Stack Overflow or by moustafa
Published on 2010-04-03T13:55:53Z Indexed on 2010/04/03 14:03 UTC
Read the original article Hit count: 301

Filed under:
|

I have this table:

fourn_category (id , sub)

I am using this code to count:

function CountSub($id){
$root = array($id);
 $query = mysql_query("SELECT id FROM fourn_category WHERE sub = '$id'");                                            

    while( $row = mysql_fetch_array( $query, MYSQL_ASSOC ) ){

        array_push($root,$row['id']);
       CountSub($row['id']);
    }        

    return implode(",",$root);  
}

It returns the category id as 1,2,3,4,5 to using it to count the sub by IN()

But the problem is that it counts this:

category 1 > category 2 > category 3 > category 4 > category 5

Category 1 has 1 child not 4. Why? How can I get all children's trees?

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql