# Php mysql hierarchy

**URL:** <https://forum.kirupa.com/t/php-mysql-hierarchy/66550>\
**Category:** Uncategorized\
**Created:** [October 6, 2004, 7:53pm UTC](https://forum.kirupa.com/t/php-mysql-hierarchy/66550 "2004-10-06T19:53:43Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmd](https://avatars.discourse-cdn.com/v4/letter/c/dbc845/32.png) [@cmd](https://forum.kirupa.com/u/cmd)\
**Post date:** [October 6, 2004, 7:53pm UTC](https://forum.kirupa.com/t/php-mysql-hierarchy/66550/1 "2004-10-06T19:53:43Z")

</div>

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

```php
 
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…

```php
 $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
