Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7837475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:39:12+00:00 2026-06-02T14:39:12+00:00

I have two functions that together build a treeview list on my website. It’s

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T14:39:13+00:00Added an answer on June 2, 2026 at 2:39 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two functions that make $.getJSON calls - one looks at JSON stored
I have two functions that have different enough logic but pretty much the same
I have two functions that are supposed to encrypt and decrypt a string but
Say I have two functions that expect ...rest parameters private function a(...myParams):void { trace(myParams.length);
I have created two functions that sum and subtract the numeric values of two
So I have two jquery functions that bassically do the same, but on 2
I have two fields that I'd like to match. (already done the validation functions
I have two functions here say animation1 and load1 they work like that: function
I have two sources of clinical procedure billing information that I have added together
I have two functions that pulling some of content from html and returning it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.