1

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
  • I mean, the data is from database. Commented Jan 22, 2010 at 9:23

2 Answers 2

2

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)"
},
[...]
Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

Both links appear dead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.