I am trying to get my data which is hierarchically set up with a tree traversal model into an < ul> in order to show on my site.
Here is my code:
Better Render Tree Function that worked for me (php function to prepare html source for use in jsTree jQuery plugin) instead of the Henrik Opel's one:
function MyRenderTree ( $tree = array(array('name'=>'','depth'=>'')) ){
$current_depth = 0;
$counter = 0;
$result = '';
foreach($tree as $node){
$node_depth = $node['depth'];
$node_name = $node['name'];
$node_id = $node['category_id'];
if($node_depth == $current_depth){
if($counter > 0) $result .= '';
}
elseif($node_depth > $current_depth){
$result .= '';
$current_depth = $current_depth + ($node_depth - $current_depth);
}
elseif($node_depth < $current_depth){
$result .= str_repeat('
',$current_depth - $node_depth).'';
$current_depth = $current_depth - ($current_depth - $node_depth);
}
$result .= '- ';
++$counter;
}
$result .= str_repeat('
',$node_depth).'';
$result .= '';
return $result;}
Result HTML: