Php method of generating a tree list from database query results, query result tree
The example in this article describes how PHP generates a tree list from the database query results. Share to everyone for your reference. The specific analysis is as follows:
This code can read data from the database to generate a tree-like List of Windows-similar resource managers
<?php/* here is the database definitions (for Solid) which I use with this code. * It should not being hard-to-adapt it to another database. *//*create TABLE dirent_types (id INTEGER not NULL, Icon varchar (#), name varchar (), PRIMARY KEY (id)); INSERT into dire Nt_types values (1, ' folderclosed ', ' Directory '), INSERT into dirent_types values (2, ' document ', ' File '); CREATE TABLE directory (ID integer not NULL, parent integer REFERENCES directory (ID), name varchar ($), Icon varchar (50) , type INTEGER REFERENCES dirent_types (id), URL VARCHAR ($), PRIMARY KEY (ID));D rop INDEX Directory_idx; CREATE UNIQUE INDEX directory_idx on directory (parent, name); CREATE SEQUENCE dirent_id; " CREATE PROCEDURE insert_dir_entry (name VARCHAR, parent INTEGER, type integer) RETURNS (id integer) BEGIN EXEC SQL whenever SQLERROR ABORT; EXEC SEQUENCE dirent_id. NEXT into ID; EXEC SQL PREPARE c_insert INSERT into directory (ID, parent, type, name) VALUES (?,?,?,?); EXEC SQL EXECUTE C_insert USING (ID, parent, type, NAME); EXEC SQL DROP C_insert; END "; Call Insert_dir_entry (' My computer ', NULL, 1); Call Insert_dir_entry (' Network neighbourhood ', NULL, 1); Call Insert_dir_entry (' lucifer.guardian.no ', 2, 1); Call Insert_dir_entry (' rafael.guardian.no ', 2, 1); Call Insert_dir_entry (' uriel.guardian.no ', 2, 1); Call Insert_dir_entry (' Control Panel ', NULL, 1); Call Insert_dir_entry (' Services ', 6, 1); Call Insert_dir_entry (' Apache ', 7, 2); Call Insert_dir_entry (' Solid Server 2.2 ', 7, 2); */function icon ($icon, $name = ", $width = 0, $height = 0) {Global $DOCU Ment_root; $icon _loc = '/pics/menu '; $file = "$DOCUMENT _root$icon_loc/$icon. gif"; if (! $width | |! $height) {$iconinfo = getimagesize ($file), if (! $width) {$width = $iconinfo [0];} if (! $height) {$heigh t = $iconinfo [1]; }} printf (", $name? "Name=\" $name \ "": ", $icon, $width, $height);} function Display_directory ($parent, $showdepth =0, $ancestors =false) {global $child _nodes, $node _data, $last _child; Reset ($child _nodes[$parent]); $size = sizeof ($child _nodes[$Parent]); $lastindex = $size-1; if (! $ancestors) {$ancestors = Array ();} $depth = sizeof ($ancestors); printf ("', $parent, $showdepth > 0? ' Show ': ' Hide '); while (list ($index, $node) = All ($child _nodes[$parent]) {for ($i = 0; $i < $depth; $i + +) {$up _parent = (int) $node _d ata[$ancestors [$i]][' parent ']; $last _node_on_generation = $last _child[$up _parent]; $uptree _node_on_generation = $ancestors [$i]; if ($last _node_on_generation = = $uptree _node_on_generation) {icon ("blank"),} else {icon ("line");}} if ($child _nodes[$node]) {//have children, i.e. it is a folder $conn _icon = "plus"; $expand = true;} else {$conn _icon = "Join"; $expand = false; } if ($index = = $lastindex) {$conn _icon. = "Bottom";} elseif ($depth = = 0 && $index = = 0) {$conn _icon. = "Top"; } if ($expand) {printf ("", $node);} Icon ($conn _icon, "Connimg_$node"); if ($expand) {print ("");} $icon = $node _data[$node] [' Icon ']; if (! $icon) {$type = $node _data[$node] [' type ']; $icon = $GLOBALS [' Dirent_icons ' [$type]; } icon ($icon, "Nodeimg_$node"); $name = $node _data[$node] [' name ']; printf ('?%s
',-1, $name, 10); if ($child _nodes[$node]) {$newdepth = $showdepth, if ($newdepth > 0) {$newdepth--;} $new _ancestors = $ancestors; $ne W_ancestors[] = $node; Display_directory ($node, $newdepth, $new _ancestors); }} print ("");} function Setup_directory ($parent, $maxdepth) {global $dirent _icons, $child _nodes, $node _data, $last _child; $dirent _ icons = Sql_assoc (' SELECT id,icon from Dirent_types '); $query = ' SELECT id,parent,type,icon,name '. ' From directory '. ' ORDER by Parent,name '; $child _nodes = Array (); $node _data = Array (); $res = SQL ($query); while (list ($id, $parent, $type, $icon, $name) =db_fetch_row ($res)) {$child _nodes[(int) $parent] [] = $id; $node _data[$id] = Array (' id ' = = $id, ' parent ' = = $parent, ' type ' + $type, ' icon ' + $icon, ' name ' and ' = $name '); $last _child[(int) $parent] = $id; }}?>
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/985265.html www.bkjia.com true http://www.bkjia.com/PHPjc/985265.html techarticle PHP from the database query results to generate a tree list of methods, query results tree This article describes the PHP from the database query results to generate a tree list method. Share to everyone for your reference ...