PDA

View Full Version : Loading TreeData all at once from Server (PHP)



genix
6 Feb 2009, 10:45 AM
Hello,

I thought, because I nowhere found this, to share some Code with you.

For doing what described in the title you first need the "ArrayTree"-Extension from Jozef Sakáloš.

It can be downloaded here: http://arraytree.extjs.eu/

Next you can begin to write your Serverside Code, I use PHP:


<?php
$result = mysql_query("Select * from `folders` where `parent`=-1");

$folders = array();

while($row = mysql_fetch_assoc($result)){
$arr['text'] = $row['name'];
$arr['children'] = getChildren($row['id']);

$folders[] = $arr;
}

function getChildren($id){
$arr = array();
$query = mysql_query("Select * from `folders` where `parent`=".$id);
while($row = mysql_fetch_assoc($query)){
$narr['text'] = $row['name'];
$narr['children'] = getChildren($row['id']);
$arr[] = $narr;
}

return $arr;
}

echo json_encode($folders);
?>
Now to the Client Code:


Ext.Ajax.request({
url: 'get-folders.php',
success: function(xhr) {
var children = Ext.decode(xhr.responseText);
var tree = new Ext.ux.tree.ArrayTree({
rootConfig: {
text: 'Root',
visible: true
},
width: 176,
height: 220,
renderTo: 'div-tree',
children: children
});
tree.expandAll();
}
});
Hope this is of some use for you!

Greetings
genix