Hey everyone, this one has me stumped…
I have a table called lwsp_categories with a column called cid which is of type int:
Parent is #22
Children of #22 are #23, #24 and #25
Children of #23 is #29
Children of #29 are #32 and #35
or graphically the tree looks like this:
22
-23
–29
—32
—35
-24
-25
What I’m trying to do is when I call get_path(22) it will print all children/subchildren/sub…sub…children of parent 22 and return an array containing the results.
Here’s my code
function get_path($start) {
$pathArray = array();
$query = "SELECT * FROM lwsp_categories WHERE parent='$start";
$result = dbSelect($query);
while ($row = mysql_fetch_array($result)) {
echo $row['cid'] . '<br>';
get_path($row['cid']);
$pathArray[] = $row[cid];
}
return $pathArray;
}
I call the function like this…
$pathArray = get_path(22);
print_r($pathArray);
The result is:
23
29
32
35
24
25
Array ( [0] => 23 [1] => 24 [2] => 25
The children are printed fine, but the function is only pushing the main children of #22 into the array, skipping all children of children, so 29, 32, and 35 are not being saved into the array.
-chris