How PHP generates a tree-shaped list from database query results _php tips

Source: Internet
Author: User
Tags php programming visibility

This example describes how PHP generates a tree list from database query results. Share to everyone for your reference. The specific analysis is as follows:

This code can read data from a database to generate a tree-like List of Windows-similar resource managers

<?php/* Here are the database definitions (for Solid) which I use in 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 Dirent_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
(m), type INTEGER REFERENCES dirent_types (id), URL VARCHAR (), PRIMARY KEY (id));
DROP 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 when
 EVER SQLERROR ABORT; EXEC SEQUENCE dirent_id.
 NEXT into ID;
 EXEC SQL PREPARE c_insert INSERT into directory (ID, parent, type, name) VALUES (?,?,?,?); EXEC SQL EXECUTEC_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 $DOCUMENT _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) {$height = $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 (' <div id= "node_%d" class= "Direntry" visibility= "%s" > ", $parent, $showdepth > 0?
 ' Show ': ' Hide '); while (the list ($index, $node) = each ($child _nodes[$parent])} {for ($i = 0; $i < $depth; $i + +) {$up _parent = (int) $node
 _data[$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]) {//has 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 ("<a href=\" javascript:document.layers[' node_%d '].visibility= ' show ' > ', $node);
 Icon ($conn _icon, "Connimg_$node");
 if ($expand) {print ("</a>");
 $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 ('? <font size= "%d" >%s</font><br%c> ',-1, $name, 10);
 if ($child _nodes[$node]) {$newdepth = $showdepth;
 if ($newdepth > 0) {$newdepth-;
 } $new _ancestors = $ancestors;
 $new _ancestors[] = $node;
 Display_directory ($node, $newdepth, $new _ancestors);
} print ("</div\n>");
 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 BYParent,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 ' =>
 $NAME);
 $last _child[(int) $parent] = $id; }}?>

I hope this article will help you with your PHP programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.