FuelPHP Bin
<?php public function dump_treesample() { // get the PK $pk = reset(static::$_primary_key); // and the tree pointers $left_field = static::tree_config('left_field'); $right_field = static::tree_config('right_field'); $name = static::tree_config('name'); // storage for the result, start with the current node $this['children'] = array(); #$tree = array($this->{$pk} => $this->to_array(true)); $tree = array(); $tree['data'] = $this->name; $tree['attr'] = array( "id" => $this->{$pk}); $tree['state'] = "open"; #\Debug::dump($tree); #die; // parent tracker $tracker = array(); $index = 0; $tracker[$index] =& $tree[$this->{$pk}]; #\Debug::dump($tracker[$index] =& $tree[$this->{$pk}]); die; // loop over the descendants foreach ($this->descendants()->get() as $treenode) { // get the data for this node $node = $treenode->to_array(true); #$node = array(); #$node['data'] = $treenode->name; #$node['attr'] = array( "id" => $treenode->id); // make sure we have a place to store child information $node['children'] = array(); // is this node a child of the current parent? if ($treenode->{$left_field} > $tracker[$index][$right_field]) { // no, so pop the last parent and move a level back up $index--; } // add it as a child to the current parent $tracker[$index]['children'][$treenode->{$pk}] = $node; // does this node have children? if ($treenode->{$right_field} - $treenode->{$left_field} > 1) { // create a new parent level $tracker[$index+1] =& $tracker[$index]['children'][$treenode->{$pk}]; $index++; } } #\Debug::dump($tracker); die; \Debug::dump($tree); #die; return $tree; }