I have two functions that together build a treeview list on my website. It's based on recursion and allows to build a treeview with an unlimited number of nodes.

But I can't make it collapsible. For example: script should determine whether $_GET['node'] == $node_id or not, and if it is, display (unroll) block element and all it's parents. So, I need to pass that "displaying" parameter on the top, to the root.

The thing is in $display and $display2 vars.

  • I have a classic db table with three columns (id, parent_id, name). Root nodes have not parent_id field filled.
  • hrefs links to just a GET parameters. GET parameter means node number.

I just need this collapsing technique working based on what node i was selected.

UPDATE: Ok, there is a complete and purified info. i created a php file, working with a database, containing only one table. just for understanding the issue:

1 I use sqlite3 database format. this is the DB dump:

        # sqlite3 catalog.sqlite .dump
    PRAGMA foreign_keys=OFF;
    BEGIN TRANSACTION;
    CREATE TABLE groups(id INTEGER PRIMARY KEY NOT NULL, name TEXT, parent_id INTEGER);
    INSERT INTO "groups" VALUES(1,'root1','');
    INSERT INTO "groups" VALUES(2,'root2','');
    INSERT INTO "groups" VALUES(3,'root3','');
    INSERT INTO "groups" VALUES(4,'root4','');
    INSERT INTO "groups" VALUES(5,'sub1',1);
    INSERT INTO "groups" VALUES(6,'sub3',3);
    INSERT INTO "groups" VALUES(7,'subsub1',5);
    INSERT INTO "groups" VALUES(8,'subsubsub1',7);
    INSERT INTO "groups" VALUES(9,'subb1',1);
    COMMIT;

2 this is an PHP file, dealing with the databse.

<?php

$db = new SQLite3('catalog.sqlite');

function build_catalog($db){ //build roots and diggs for childnodes for every root in a loop
    //$content_root="<ul id='collapsedlist'>";
    $content_root = '';
    $roots = $db->query('SELECT * from groups WHERE parent_id="" OR parent_id is null');
    while($root = $roots->fetchArray()){
        list ($content,$display)=get_children_of_node($db,$root['id']); 
        $content_root .= "<li id='".$root['id']."' ><a href='/?node=".$root['id']."'>".$root['name']."</a>";
        $content_root .= $content;
        $content_root .= "</li>\n";
    }
    $content_root = "<ul id='collapsedlist'>".$content_root."</ul>\n";

    return $content_root;
}

function get_children_of_node($db,$node_id){
    if(!isset($content)) $content = '';
    $display = (isset($_GET['node']) && $_GET['node'] == $node_id)? "style='display:block'" : "style='display:none'";
    $query = $db->querySingle('SELECT count(*) from groups WHERE parent_id='.$node_id);
    if ($query > 0){
        //$content .= "<ul class='subcategories'>\n";
        $children = $db->query('SELECT * from groups WHERE parent_id =\''.$node_id.'\'');
        while ($child = $children->fetchArray()){
            list($content2,$display)=get_children_of_node($db,$child['id']);
            $content .= "<li id='".$child['id']."' ".$display.">";
            $content .= "<a href='/?node=".$child['id']."'>".$child['name']."</a>";
            $content .= $content2;
            $content .= "</li>\n";
        }
        $content = "<ul class='subcategories' ".$display.">".$content."</ul>\n";
    }
    return array($content,$display);
}


?>

Here the php file ends with a pure HTML shoving above. i divided it one from another here, hence the editor cant parse HTML+PHP syntax at once. but it is the same index.php file. HTML part:

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Collapsible Nested List</title>
    </head>
    <body>
        <div id="sidebar">
            <?=build_catalog($db);?>
        </div>
    </body>
    </html>
有帮助吗?

解决方案

Here is a quick solution to your problem, keeping the same HTML just replace your PHP with the following code.

function get_children($db, $parent_id) {
    $res = $db->query("SELECT * FROM groups WHERE parent_id='$parent_id'");
    if (!$res) return array();

    $out = array();
    while ($row = $res->fetchArray(SQLITE3_ASSOC)) $out[$row['id']] = $row;
    return $out;
}

function get_parent_id($db, $node_id) {
    return $db->querySingle("SELECT parent_id FROM groups WHERE id='$node_id'");
}

function get_menu($db, $node_id) {
    $menu = get_children($db, $node_id);
    while (($parent_id = get_parent_id($db, $node_id)) !== null) {
        $temp = get_children($db, $parent_id);
        $temp[$node_id]['children'] = $menu;
        $menu = $temp;
        $node_id = $parent_id;
    }
    return $menu;
}

function build_html(array $menu) {
    $str = '';
    foreach ($menu as $id => $item) {
        $str .= sprintf('<li><a href="?node=%s">%s</a></li>', $id, $item['name']);
        if (isset($item['children']))
            $str .= build_html($item['children']);
    }
    return "<ul>$str</ul>";
}

function build_catalog($db) {
    $menu = get_menu($db, isset($_GET['node']) ? intval($_GET['node']) : '');
    return build_html($menu);
}

This code could be really optimized if your "groups" table is quite small. An idea would be to get all records in a $groups array and to build a parent_id index. The building of the catalog would then be much more easy.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top