Search code examples
phptreeviewdatasource

Is there PHP treeview with data from database?


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?


Solution

  • 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)"
    },
    [...]