problem in counting children category
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?
From what I understand, I suspect that you have a scope issue in your code. Try :
function CountSub($id, &$current=array()){
array_push($current, $id);
$query = mysql_query("SELECT id FROM fourn_category WHERE sub = '$id'");
while( $row = mysql_fetch_array( $query, MYSQL_ASSOC ) ){
CountSub($row['id'], $current);
}
return implode(",",$current);
}
CountSub(1)
In your code, a new $root array is created at each level of the recursive function and the recursive calls never modify the original created array, which may be your problem
I Hope this will help Jerome
精彩评论