Note that there are some explanatory texts on larger screens.

plurals
  1. POis it possible to build a collapsible treeview list using PHP only (no JS)
    primarykey
    data
    text
    <p>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.</p> <p>But I can't make it collapsible. For example: script should determine whether <code>$_GET['node'] == $node_id</code> 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.</p> <p>The thing is in <code>$display</code> and <code>$display2</code> vars.</p> <ul> <li>I have a classic db table with three columns (id, parent_id, name). Root nodes have not parent_id field filled. </li> <li>hrefs links to just a GET parameters. GET parameter means node number.</li> </ul> <p>I just need this collapsing technique working based on what node i was selected.</p> <p>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:</p> <p>1 I use sqlite3 database format. this is the DB dump:</p> <pre> # 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; </pre> <p>2 this is an PHP file, dealing with the databse.</p> <pre><code>&lt;?php $db = new SQLite3('catalog.sqlite'); function build_catalog($db){ //build roots and diggs for childnodes for every root in a loop //$content_root="&lt;ul id='collapsedlist'&gt;"; $content_root = ''; $roots = $db-&gt;query('SELECT * from groups WHERE parent_id="" OR parent_id is null'); while($root = $roots-&gt;fetchArray()){ list ($content,$display)=get_children_of_node($db,$root['id']); $content_root .= "&lt;li id='".$root['id']."' &gt;&lt;a href='/?node=".$root['id']."'&gt;".$root['name']."&lt;/a&gt;"; $content_root .= $content; $content_root .= "&lt;/li&gt;\n"; } $content_root = "&lt;ul id='collapsedlist'&gt;".$content_root."&lt;/ul&gt;\n"; return $content_root; } function get_children_of_node($db,$node_id){ if(!isset($content)) $content = ''; $display = (isset($_GET['node']) &amp;&amp; $_GET['node'] == $node_id)? "style='display:block'" : "style='display:none'"; $query = $db-&gt;querySingle('SELECT count(*) from groups WHERE parent_id='.$node_id); if ($query &gt; 0){ //$content .= "&lt;ul class='subcategories'&gt;\n"; $children = $db-&gt;query('SELECT * from groups WHERE parent_id =\''.$node_id.'\''); while ($child = $children-&gt;fetchArray()){ list($content2,$display)=get_children_of_node($db,$child['id']); $content .= "&lt;li id='".$child['id']."' ".$display."&gt;"; $content .= "&lt;a href='/?node=".$child['id']."'&gt;".$child['name']."&lt;/a&gt;"; $content .= $content2; $content .= "&lt;/li&gt;\n"; } $content = "&lt;ul class='subcategories' ".$display."&gt;".$content."&lt;/ul&gt;\n"; } return array($content,$display); } ?&gt; </code></pre> <p>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:</p> <pre><code> &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt; &lt;title&gt;Collapsible Nested List&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="sidebar"&gt; &lt;?=build_catalog($db);?&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload