Id | ParentId | PathString | Name | Depth
---+----------+-------------+--------------+------
87 | 2 | /87/ | Countries | 2
88 | 87 | /87/88/ | Belgium | 3
89 | 87 | /87/89/ | Netherlands | 3
91 | 89 | /87/89/91/ | Business | 4
92 | 89 | /87/89/92/ | Economy | 4
90 | 87 | /87/90/ | Germany | 3
Selecting the path:
function select_path( id )
{
$info =
SELECT *
FROM tree
WHERE Id = $id;
$parts =
array_slice( split( '/', $info['PathString'] ), 1, -1 );
$path =
SELECT Id, Name
FROM tree
WHERE Id in $parts;
return $path;
}