Location: PHPKode > scripts > HTML dbtree > html-dbtree/sample.php
* This script is a really basic sample of using htmldbtree, 
* you will have to look at the doc for more info on what to do. 
* Just check the methods name, i've tried to made them self-explaining.
* At this time and as i don't work with E_ALL error reporting,
* you will certainly have errors if you use it. 
* Please don't complain just send me your error logs and i'll correct them asap.
* you should visit the link in the class file to have a good overview of the theory used here (really intersting reading).

// u must use mysqldb class of mine to get it work at this time
// but i'm sure u can easily modify to use your own database object.


$DB_NAME = 'test';
$DB_HOST = 'localhost';
$DB_USER = 'root';
$DB_PASS = '';
# $table_name = 'data';
$table_name = 'data';

$db = & new mysqldb($DB_NAME,$DB_HOST,$DB_USER,$DB_PASS);
# map your database fields as needed
$field_map = array('id'=>'id', 'parent'=>'parent_id', 'left_id'=>'left_id','right_id'=>'right_id','level'=>'level');

# create the tree object
$tree = &new HtmldbTree($db,$table_name,$field_map,TRUE);
# then work with your tree.
# rebuild the left and rights ids and level fields based on the parent field
# (you generally do that when you want to get an old tree based only on parent id to make it compliant with htmldbtree)

# then add a root node
# htmldbtree::add(array('db_field_name'=>'data_value','db_field_name2'=>...),$parentid);
echo "<br/><br/>insert root node<br />\n";
$rootnode = $tree->add(array('label'=>'root node'),0);

# and some more for the example
  $datas = array('label'=>"node label #$i",'desc'=>"desc #$i",'url'=>"url #$i",'icon'=>'<img src=js/ThemeIE/folder.gif>');
  $node[$i] = $tree->add($datas,$rootnode);
  echo "inserted node id ".$node[$i]." as child of rootnode<br />\n";

# you can move it to a new parent
echo "reparent some nodes\n";

# now you can access prev and next brothers of node like this
echo "<hr> Brothers of node ".$tree->datas[$node[6]]['label']."<br/>";
$prev = $tree->prevbrother($node[6]);
$next = $tree->nextbrother($node[6]);
echo "previous brother of ".$tree->datas[$node[6]]['label']." is " .$tree->datas[$prev]['label']."<br/>\n";
echo "next brother of ".$tree->datas[$node[6]]['label']." is " .$tree->datas[$next]['label']."<br/>\n";

# then moveup / down some node as requested by Bruno Gaspar

# then print the tree structure
# echo "<pre><xmp>---------------------------------------\nTree Strucure\n";
# print_r($tree->get_childs(FALSE));
# echo "-------------------------------------------</xmp></pre>";
# or as html 
echo "<br/><br/>render the tree as html<br/>";
$tree_datas = $tree->get_tree(FALSE); # assume your root node id is 0
foreach($tree_datas as $node_datas){
  echo str_repeat('&nbsp;&nbsp;',$node_datas['relative_level'])."$node_datas[label]<br>\n";

echo "<br/><br/>render as a select element<br/>\n";
# get indented html options for select items
# htmldbtree::html_get_options('db_shown_field', 'db_value_field', $selected_value);
$selected_value = '';
echo "<select>".$tree->html_get_options('label', 'id', $selected_value)."</select>\n<Br/>\n";

some people ask for details about html_get_path method so here it is:
I personnaly use this method to display simple html path to the given node,
as when dealing with categories on a website. 
- $linkexp is an expression with fields replacement
  suppose that links thru categories have url like this: index.php?cat=int_catid
so $linkexp = "index.php?cat=%=id=%"
where %=id=% will be replace by the id of each tree node in the path
you can use %=id=%, %=left_id=% ,%=right_id=%, %=level=% or any of the field a node can contain. (list of field in the database)
- $labelfield is the field you want to be used as the link label
- $item is the id of the node you want to retrieve path to
- $keep_item let you set if the the given node will be part of the path or not
- $stringlue is the string you want to use to separate path elements
this would return you something like this:
<a href='index.php?cat=1'>root_node</a>::<a href='index.php?cat=2'>parent_node</a>::<a href='index.php?cat=3'>item_node</a>

echo "<br/><br/>render as a sort of quick navigation with html_get_path<br/>\n";
echo $tree->html_get_path($linkexp="test.php?id=%id%",$labelfield='label',$item=$node[8],$keep_item=TRUE,$stringglue=' / ');

//see the doc for more details

<SCRIPT LANGUAGE="JavaScript" SRC="js/JSCookMenu.js"></SCRIPT>
<LINK REL="stylesheet" HREF="js/ThemeIE/theme.css" TYPE="text/css">
<SCRIPT LANGUAGE="JavaScript" SRC="js/ThemeIE/theme.js"></SCRIPT>
# NEW JSCOOKMENU GENEREATION SAMPLE (http://www.cs.ucla.edu/~heng/JSCookMenu/index.html) requested by Marco Valk
# if you want to use the jscoolmenu generator you have to take care of some more fields mapping
# for this sample this is automaticly done as fields have the defaults name in the database.
# fields to map for jscookmenu: label, icon, desc, target, url
echo "<hr>JSCookMenu sample<br>".$tree->cookmenu('myMenu',$rootnode);

# or even better you can use a callback to create the label, icon, desc, target, url from other fields like this: 
function item_mapping($item){
  if( in_array($item['label'],array('node label #2','node label #9','node label #10') ) ) # you can remove nod by returning false
    return FALSE;
  $isfolder = (bool) $item['number_of_children'];
  $item['icon'] = $isfolder?'<img src=js/ThemeIE/folder.gif>':'';
  $item['url']  = $isfolder?'':'mypage.php?item='.$item['id'];
  $item['desc'] = 'Description: '.$item['label'];
  return $item;
echo "<hr>JSCookMenu sample with data mapping<br>".$tree->cookmenu($jsname='myMenu2',$rootparentnode=$rootnode,$depth=FALSE,'item_mapping');
# then you have to load your menu like this, see jscookmenu site for more detail on this
<SCRIPT LANGUAGE="JavaScript"><!--
	cmDraw ('myMenu', myMenu, 'vbr', cmThemeIE, 'ThemeIE');
	cmDraw ('myMenu2', myMenu2, 'hbr', cmThemeIE, 'ThemeIE');
Return current item: HTML dbtree