Building a Tree with PHP connector

Hello,

I want to build a tree using the PHP connector. For this exemple i use the sample code of the tree connector. All i want to do is implement some “if’s” on each one of the cases.

  • How can i customize my tree depending of the ID’s loaded from de database. I want to change the item colour, remove the item (don’t show him) or replace him by a item with an ID and text specified by me?
  • How can i change the query that i will execute depends of the parameter that i receive in the URL?

Here is my PHP code:

[code] require_once(“…/config.php”);
$res=mysql_connect($mysql_server,$mysql_user,$mysql_pass);
mysql_select_db($mysql_db);

require("../../codebase/treemultitable_connector.php");


function custom_format($item){
	if ($item->get_value("project_id")>1){ 
		
		if ($item->get_value("project_id")=2){ 
			$item->set_image("lock.gif"); 	//Change the icon image 
			$item->set_kids(false); 		//Prevent the existence of kids/parents
		}else{
			if($item->get_value("project_id")=3){
				//replace the item by Name=NewProject and ID=100
			}else{
				//Delete item
			}
		}
	}else{
		//Show normal item (without any change)
	}
}


$tree = new TreeMultitableConnector($res);
$tree->enable_log("temp.log",true);
$tree->setMaxLevel(3);
$level = $tree->get_level();

switch ($level) {
	case 0:
		$tree->event->attach("beforeRender",custom_format());
		$tree->render_table("projects","project_id","project_name","","");
		break;
	case 1:
		//If an URL parameter is set i want to run a diffrent query with a specific parameter
		if($_REQUEST['specific_team']>0){
			$team = $_REQUEST['specific_team']; 
			$tree->render_sql("SELECT teams.team_id, teams.team_name, project_team.project_id FROM teams WHERE team.team_id = $team INNER JOIN project_team ON teams.team_id=project_team.team_id", "team_id", "team_name", "", "project_id");
		}else{
			$tree->render_sql("SELECT teams.team_id, teams.team_name, project_team.project_id FROM teams INNER JOIN project_team ON teams.team_id=project_team.team_id", "team_id", "team_name", "", "project_id");
		}
		break;
	case 2:
		$tree->render_table("developers", "developer_id", "developer_name", "", "developer_team");
		break;
	case 3:
		$tree->render_table("phones", "phone_id", "phone", "", "phone_developer");
		break;
}[/code]

I add some comments in the php file where i want to do the chenges:
//Change the icon image
//Prevent the existence of kids/parents
//replace the item by Name=NewProject and ID=100
//Delete item
//If an URL parameter is set i want to run a diffrent query with a specific parameter

NOTES: The config is working, and the html is working to.

HTML:

[code]

Multitable connector GetItemID
[/code]

I noticed that the $item->get_value(“project_id”) is not working. I don’t now if i’m doing it well.

How can i customize my tree
You can use beforeRender event to customize items or skip them at all
docs.dhtmlx.com/doku.php?id=dhtm … nder_event
docs.dhtmlx.com/doku.php?id=dhtm … xgrid#skip

How can i change the query that i will execute depends of the parameter
Just use any php code before render_sql command to build necessary sql

The code which you are using mostly fine, but you are have it incorrect when attaching event, it must be

$tree->event->attach("beforeRender",custom_format); //just name of function, withou brackets

Thanks for the info.
I already solve the problem of the url parameter and i already detect my problem. I has defined the “event” inside the switch operator, and it has to be defined before the switch.

Now i have a problem inside of the custom_format() function.
I want to proceed in diffrent ways dependig of the root parents, but i don’t know how can i access them.
For example,

  • On the case 2 (level 2), i want to set the item image or skip() the item if the root ID of level 0. How can i achieve that information? Something like :
function custom_format($item){
	...
	...
	$SearchOnLevel = 0;
	if($item->get_root_parent_id( $SearchOnLevel )){
		$item->set_image("lock.gif");
	}
}

Is it possible?

If you need to define different rendering strategies per level - you can define different beforeRender event handlers for different sections of the switch.

If you need to use different logic based on parent id - $item->get_parent_id() will work correctly, this command do not require any extra params, and will return parent id of the current item.

docs.dhtmlx.com/doku.php?id=dhtm … _parent_id