Location: PHPKode > scripts > Tree Class > tree-class/example_Tree.php



$tree = new Tree($ddata,$null);
			echo "<pre>";
			echo "</pre>";
     //create a root node by passing a variable with value null,

//returns a reference to the root node

  $null = null;

// generate a new tree, 
// the root elements are identified by null value 
// there is no data (second parameter)
    $tree = new Tree($null);
// another possibility is to hand over a reference to a parent

// always use references (&)! 
     $node = &$tree->addChild(array("key0"=>"Home"));

  // create another node as subnode of $node
    $subnode = &$node->addChild(array("key0"=>"Home sub1"));
    $subnode = &$node->addChild(array("key1"=>"Home sub2"));

  // create another node as subnode of root node 
    $node = &$tree->addChild(array("key1"=>"Work"));
    $subnode = &$node->addChild(array("key0"=>"Work sub1"));
    $subnode = &$node->addChild(array("key1"=>"Work sub2"));

  		$dummydata = array("dummykey"=>"dummyvalue");
    	// data load may be anything, here we use the dummydata array 
    	$subsubnode = &$subnode->addChild($dummydata);
    	// some information about the node:
    	echo 'the added node has the ID: '.$subsubnode->getId().'<br>';
    	// lets print the level :
    	echo ' created node is on level: '.$subsubnode->getLevel().'<br>';

		// path 
		echo 'path to it from root node is (Array of ids): ';
		echo '<br>';

    	//get the  parent

	// get the root node (reference!) 

    // have a look on the structure
echo '<h1>Tree structure</h1>';
echo '<pre>';
echo '</pre>';

echo 'delete a node (and child nodes!), path to it from root node is: ';


echo 'the new structure: ';
echo '<pre>';
echo '</pre>';

Return current item: Tree Class