views:

290

answers:

2

Hello,

I want to know if there exists php treeview with data from mysql. I haven't found a suitalbe one for my project. Do you know if there is some plugins or code samples out there?

Thanks a lot.

Edit:

jQuery Treeview's asyncronous example, link text

I found it can work, but i don't know how to get the source.php. Do you have any ideas or other propositions?

+1  A: 

you would need to run the query yourself, but it's pretty easy. the output the tree expects is an array of objects in json format like the example below.

your table structure could be:

tree_node (id, title, parent_id)

you would select the root node, then it's children, recursively until the tree is complete.

function expandTree($node)
{
  $result = array('text' => $node['title'], 'children' => array());
  $nodes = getChildren($node);  // query all nodes whose parent_id = $node['id']
  foreach ($nodes as $node) {
    $result['children'][] = expandTree($node);
  }
  return $result;
}

output format:

[
{
    "text": "1. Pre Lunch (120 min)",
    "expanded": true,
    "classes": "important",
    "children":
    [
        {
            "text": "1.1 The State of the Powerdome (30 min)"
        },
        {
            "text": "1.2 The Future of jQuery (30 min)"
        },
        {
            "text": "1.2 jQuery UI - A step to richnessy (60 min)"
        }
    ]
},
{
    "text": "2. Lunch  (60 min)"
},
[...]
jspcal
+1  A: 

Assuming you have a db with parents and children, have a look at

http://www.ideashower.com/our_solutions/create-a-parent-child-array-structure-in-one-pass/ & http://www.phpriot.com/articles/nested-trees-1

Once you have your data correctly sorted, you can then look at rendering it.

devians
@thanks a lot, it's helpful and i'm learning that.
garcon1986