Reputation: 588
below is my SQL table
id parent_id
1 0
2 0
3 1
4 1
5 3
6 5
i want to display n'th level hierarchic relation in array like below
array
{
1
sub{
3
sub{
5
}
4
}
}
and so on
how can i do this in PHP
Upvotes: 0
Views: 2399
Reputation: 3065
first select all your root categories with parent id 0 and pass their ids to this recursive function
function getChildCats($catId)
{
$sql = "select * from categories where parent_id = $cateID";
$res = mysql_query($sql);
$raws[];
while($raw = mysql_fetch_assoc($res))
{
$raw['sub'] = getChildCats($raw['id'])
$raws[] = $raw;
}
return $raws;
}
Upvotes: 1