Example of infinite tree structure implementation in php

Source: Internet
Author: User


Php implements an infinitely hierarchical tree to Implement Level 2 capabilities in the main menu. You can also obtain sub-comments under the parent comment when developing a message board! This is the function to implement an infinite tree! We will not talk much about it. Please refer to the following database structure

Create table 'emlog _ hudhf' ('id' mediumint (10) unsigned not null AUTO_INCREMENT, 'sslog' varchar (64) not null, 'Sid' varchar (64) not null,'s 'varchar (64) not null, 'Z' varchar (64) not null, 'n' text not null, 'puser' varchar (64) not null, 'buser' varchar (64) not null, primary key ('id') ENGINE = MyISAM AUTO_INCREMENT = 128 default charset = utf8 AUTO_INCREMENT = 128; -- export the data in the table 'emlog _ hudhf' -- INSERT INTO 'emlog _ hudhf' VALUES (46, '5', '0', '000000', '0', 'commented! ', '1', '1'); insert into 'emlog _ hudhf' VALUES (82, '13', '0', '123', '0 ', 'What and what ', '1', '1'); insert into 'emlog _ hudhf' VALUES (48, '6', '0', '123 ', '0', 'Do you think about comments? ', '123', '123'); insert into 'emlog _ hudhf' VALUES (70, '8', '69 ', '123456', '0', 'I replied to you again', '1', '1'); INSERT INTO 'emlog _ hudhf' VALUES (1397983195, '8 ', '66 ', '000000', '0',' I will reply to you too ', '1', '000000'); INSERT INTO 'emlog _ hudhf' VALUES (66, '8', '0', '20170', '0', 'I want to comment on you XXXXXXXXHappySky', '123', '1 '); insert into 'emlog _ hudhf' VALUES (68, '7', '0', '20170', '0', 'Test comment function recoverable ', '1 ', '123'); insert into 'emlog _ hudhf' VALUES (69, '8', '65', '123', '0 ', 'reply "what "~~~~ ', '000000', '1'); insert into 'emlog _ hudhf' VALUES (497, '8', '0', '000000', '0 ', 'Let me also comment on my own things ', '1', '1'); insert into 'emlog _ hudhf' VALUES (121, '8', '0 ', '20170', '0', 'Taobao', '1', '1'); insert into 'emlog _ hudhf' VALUES (1398561255, '8', '20170 ', '20170', '0', 'Loan', '1', '1'); insert into 'emlog _ hudhf' VALUES (1398561490, '8 ', '66 ', '000000', '0', 'Fuck your mom B', '1', '20170 ');


 
This is the image structure of the database.
 
 
The idea is as follows: if sid is 0, it indicates that the parent comment is not 0, it means the sub-comment! How can I know that the sub-comment belongs to the parent comment? If the sid is not 0, query the value of this sid to be equal to the value of this value. Let's see the column below.

Insert into 'emlog _ hudhf' VALUES (120, '8', '0', '20170', '0', 'Let me also comment on my own things ', '1', '1'); insert into 'emlog _ hudhf 'values (124, '8', '123', '123', '0 ', 'Okay ', '1', '1 ');


The sid of the second is equal to the id of the first article, so the two are the first comments!
 
And how to output it. Of course, you must first obtain the SQL statement of the data you want to output.
 

// What does this paragraph mean. Sslog is equal to the value of whidi, and is displayed according to the latest insert. $ gg = mysql_query ("SELECT * FROM ". DB_PREFIX. "hudhf WHERE sslog = '". $ whidi. "'Order by id desc"); $ data = array (); while ($ rs = mysql_fetch_assoc ($ gg) {$ data [] = $ rs; // convert it into an array and output it to another function.} getTree ($ data, 0, $ whidi, $ whiuser);} echo getTree ($ data, 0 ); function getTree ($ data, $ pId) {$ html = ''; foreach ($ data as $ k => $ v) {if ($ v ['Cate _ parentid'] = $ pId) {// find your son $ html. ="


 
Then this is the result of html beautification.


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.