recursive function to get all the child categories
Posted
by user253530
on Stack Overflow
See other posts from Stack Overflow
or by user253530
Published on 2010-03-07T23:15:41Z
Indexed on
2010/03/07
23:26 UTC
Read the original article
Hit count: 444
Here is what I'm trying to do: - i need a function that when passed as an argument an ID (for a category of things) will provide all the subcategories and the sub-sub categories and sub-sub-sub..etc. - i was thinking to use a recursive function since i don't know the number of subcategories their sub-subcategories and so on so here is what i've tried to do so far
function categoryChild($id) {
$s = "SELECT * FROM PLD_CATEGORY WHERE PARENT_ID = $id";
$r = mysql_query($s);
if(mysql_num_rows($r) > 0) {
while($row = mysql_fetch_array($r))
echo $row['ID'].",".categoryChild($row['ID']);
}
else {
$row = mysql_fetch_array($r);
return $row['ID'];
}
}
If i use return instead of echo, i won't get the same result. I need some help in order to fix this or rewrite it from scratch
© Stack Overflow or respective owner