Start Building Professional
Web Apps Today


 
Categories Question details Back To List
Question  posted by BulletXt on Aug 31, 2009 09:10
open dhtmlx forum
Create and load a dhtmlxtree from a php array

As topic says, I have a php file that does a query on a mysql server. It returns an array and I store it inside a variable. I'de like my dhtmlxtree to be created based on it's contents. What would be the best method for achieving this?

Thanks a lot


ps: if possible I want to avoid creating any permanent text file on hard disk (possibly do everything via RAM).
Answer posted by Alex (support) on Aug 31, 2009 09:52

Tree can be loaded from xml sream . You can generate this stream using PHP - php script can fetch data from database and generate xml output:

tree.loadXML("your.php")

The php sample - dhtmlxTree/samples/loading_processing_data/tree_dyn_loading.html

Answer posted by BulletXt on Sep 01, 2009 01:29
Thanks for your reply.

If possible one last question. After I do the query in php I create an XmlWriter object that creates an xml based on the query. My problem is that I can't do a print of the XmlWriter object. Is there a way to convert it for example to string? Thanks a lot in advance and sorry if the question may not be strictly related to dhtmlxtree.
Answer posted by Alex (support) on Sep 01, 2009 01:45

There is dhtmlxAjax - a ready solution for Ajax requests. It privides API to serialize xml object to string - doSerialization() method:

Please, see the example: 

dhtmlxAjax/samples/samples_of_usage/send_request.html ( http://dhtmlx.com/docs/products/dhtmlxAjax/samples/samples_of_usage/send_request.html )

Answer posted by BulletXt on Sep 01, 2009 02:00
So, I have to copy that script example code in the index.html file and replace process.php? with the name of the php file that generates the xmlwriter object? Is this correct?
Answer posted by Alex (support) on Sep 01, 2009 02:10
process.php generates xml stream. Please, see the example. 
Answer posted by BulletXt on Sep 01, 2009 07:30
Thanks for all your help. At the end I decided to use another method which is  "Building tree with script". Everything seems to work fine, but if possible I have a question. Let's say I do this to add a child:
tree.insertNewChild(1,11,"Child 1-1");


How can I do to associate to that child an http url, so that if I click on it it then opens an http url?

Thanks,

Marco
Answer posted by Alex (support) on Sep 01, 2009 07:57

If there is an array of urls that are associate with item ids, you can set onClick event handler to call the necessary function on item click:

tree.attachEvent("onClick",function(itemId){

openWindow(itemId)

})

Where openWindow is your function.

There is also userdata functionality, but probably in case of JS initialization you can use just an associate array:

tree.setUserData(itemId,"url",url_value)

var url = tree.getUserData(itemId,"url")

Answer posted by BulletXt on Sep 01, 2009 09:19
ok so this is an example. I create a root node and a child item inside the node. I want the child item to have a link when I click on it.

This is what I write to create the root node and child item:

tree.insertNewChild(0,1,"Root"); //create root node
tree.insertNewChild(1,1,"Child 1"); //create child node


Now, if that is correct (and it does work in browser), as said I'de like to add a link to Child 1. I'm doing it this way but It's not working:


    var url = tree.getUserData(1,1,"Child 1");
    tree.setUserData(1,1,"url","login_success.php");


Is this code wrong and is it missing something?


Thanks so much as always

Answer posted by Alex (support) on Sep 01, 2009 09:41

If you want to set userdata fo item with id=1, you can use the following code:

tree.setUserData(1,"url","login_success.php");

and this one to get it - please, see tree API for details dhtmlxTree/doc/alpha.html:

var url = tree.getUserData(1,"url");

Answer posted by BulletXt on Sep 02, 2009 02:12
Ok,everything seems to be working fine.

I have one small problem though. I'm not sure how to add root items.

For example I want to have 3 "folders" at root position.

I tried with this but it doesn't work:

tree.insertNewChild(0,0,\"{$array[0][0]}\");
tree.insertNewChild(1,0,\"{$array[0][1]}\");
tree.insertNewChild(2,0,\"{$array[0][1]}\");


Thanks
Answer posted by Alex (support) on Sep 02, 2009 02:34

 tree.insertNewChild(0,1,"Root 1");
 tree.insertNewChild(0,2,"Root 2");
 tree.insertNewChild(0,3,"Root 3");

The details can be found in the documentation  dhtmlxTree/doc/alpha.htm

Answer posted on Sep 02, 2009 03:01
To add a child in Root 1:  tree.insertNewChild(0,1,"Root 1");

Would I do tree.insertNewChild(1,0,"Child 1");  or  tree.insertNewChild(1,1,"Child 1");  ?
Answer posted by Alex (support) on Sep 02, 2009 03:49

Ids should be unique in tree. 0 is id of top level item. So, you can't use it again.

The example tree.insertNewChild(1,"1-1","Child 1")

Answer posted by BulletXt on Sep 02, 2009 08:21
Great now it's working :) Thanks!