Original from: http://blog.9cbs.net/ziyou_jo/archive/2005/01/20/260456.aspx code: --------------------- -------------------------------------------------- ------- Create Table `Tree1` (` ID` Tinyint (3) unsigned not null auto_increment, `Parentid` Tinyint (3) unsigned not null default '0',` Topic` VARCHAR (50) Default NULL, PRIMARY Key (`ID`), key` parentid` (`Parentid`) Type = Myisam; Insert Into` Tree1` (`Id`,` Parentid`, `Topic`) Values (1, 0, ' 1 '), (2, 0,' Tree 2 '), (3, 0,' Tree 3 '), (4, 2,' Tree 2-1 '), (5, 4,' Tree 2-1- 1 '), (6, 2,' Tree 2-2 '), (7, 1,' Tree 1-1 '), (8, 1,' Tree 1-2 '), (9, 1,' 1-3 '), (10, 8,' Tree 1-2-1 '), (11, 7,' Tree 1-1-1 '), (12, 11,' Tree 1-1-1-1) '); ----------------------------------------------- -------------------------------- Field Description ID, recorded ID number ParentID, recorded parent record ID (for 0 is a root record) Topic, recorded display title display program sequence tree: PHP code: ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ -------------------------------------------------- / * Database connection * / mysql_connect (); mysql_select_db ('Tree'); / * Tree display recursive function * / function trentid = 0) {/ * Execute SQL query, get the title of the record and ID * / $ sql = "SELECT TOPIC, ID from tree1 where parentid = $ pentid Order By ID ASC"; $ r = mysql_query ($ SQL); / * Indent * / echo ("
-------------------------------------------------- ------------------------------ Reverse sediment tree: PHP code: -------------- -------------------------------------------------- ---------------- / * Database connection * / mysql_connect (); mysql_select_db ('Tree'); / * Tree display recursive function * / function tree ($ ParentID) = 0) {/ * Execute SQL query, get recorded title and ID * / $ sql = "SELECT TOPIC, ID from tree1 where parentid = $ pentid order by id desc"; $ = mysql_query ($ sql); / * Indenge * / echo ("
Data structure (as an example of mysql) code: ------------------------------------------------------------------------------------------------- ---------------------------------------- Create Table `Tree2` (` ID` Tinyint (3) unsigned NOT NULL auto_increment, `parentid` tinyint (3) unsigned NOT NULL default '0',` rootid` tinyint (3) unsigned NOT NULL default '0', `layer` tinyint (3) unsigned NOT NULL default ' 0 ', `Orders` Tinyint (3) Unsigned Not Null Default' 0 ',` Topic` VARCHAR (50) Default Null, Primary Key (`Parenti), Key` Parentid` (`Parentid`), Key` rootid` (`rootid`). Type = myisam insert Into` Tree2` (`ID`,` Parentid`, `rootid`,` topic`) Values (1,0,1,0,0 , 'Tree 1'), (2, 0, 2, 0, 0, '), (3, 0, 3, 0, 0,' Tree 3 '), (4, 2, 2, 1, 2, 'Tree 2-1'), (5, 4, 2, 2, 3, 'Trees 2-1-1'), (6, 2, 2, 1, 1, 'Tree 2-2'), (7, 1, 1, 1, 4, 'Tree 1-1'), (8, 1, 1, 1, 2, 'Tree 1-2'), (9, 1, 1, 1, 1, ' Tree 1-3 '), (10, 8, 1, 2, 3,' Tree 1-2-1 '), (11, 7, 1, 2, 5,' Trees 1-1-1 '), ( 12, 11, 1, 3, 6, 'Tree 1-1-1-1'); ---------------------------- -------------------------------------------------- - Display program PHP code: ----------------------------------- -------------------------------------------- / * Database Connection * / mysql_connect (); mysql_select_db ('Tree'); / * Select all root record ID * / $ sql = "select id from trees"; $ = mysql_query ($ sql) Echo ("
$ RA [0] Order by Orders "; $ r1 = mysql_query ($ SQL); While ($ RA1 = MySQL_FETCH_ROW ($ RS1)) {/ * indent display * / if ($ RA1 [1]> $ lay) { Echo (Str_Repeat ("