Unlimited category and disk file directory Printing

Source: Internet
Author: User

1. Unlimited Classification
-- Create a table
Create Table it_category (
Cat_id int unsigned primary key auto_increment,
Cat_name varchar (50) default '',
Sort_order int default 0,
Parent_id int unsigned
);
-- Insert record
Insert into it_category values (75, 'classification KKK ', 50, 0 );
Insert into it_category values (12, 'classification fff', 50, 75 );
Insert into it_category values (77, 'classification aaa', 50, 75 );
Insert into it_category values (34, 'classification XXX', 50, 12 );
Insert into it_category values (35, 'classification hhh', 50, 12 );
Insert into it_category values (9, 'classification qqq', 50, 77 );
Insert into it_category values (28, 'classification jjj ', 50, 35 );
Insert into it_category values (19, 'category mmm', 50, 35 );

<? PHP
// 1. This is what the teacher said
// First, obtain all the classified data
Mysql_connect ('127. 0.0.1: 808080', 'root', '1234abcd ');
$ SQL = "select * From itcast_shop.it_category where 1 order by sort_order ";
$ Result = mysql_query ($ SQL );
While ($ ROW = mysql_fetch_assoc ($ result )){
$ List [] = $ row;
}

// Then, call the recursive lookup Function
$ Tree = gettree ($ list, 0, 0 );

// Finally, traverse and display
Foreach ($ tree as $ row ){
Echo str_repeat ('& nbsp;', $ row ['deep ']);
Echo $ row ['cat _ name'];
Echo '<br> ';
}

Function gettree ($ arr, $ p_id, $ deep = 0 ){
Static $ tree = array ();
Foreach ($ arr as $ row ){
If ($ row ['parent _ id'] = $ p_id ){
$ Row ['deep '] = $ deep;
$ Tree [] = $ row;
// Locate a qualified record and recursively look for it in a deeper level! Search and return to continue this foreach
Gettree ($ arr, $ row ['cat _ id'], $ deep + 1 );
}
}

// The result is returned after the foreach operation is completed.
Return $ tree;
}


--------------------------------- Change the method ------------------------------


<? PHP

// 2. A friend is confused by the return $ tree of the recursive function;
// Actually, this return is useless after recursion, because $ tree is already a static local variable.
// Return indicates that the foreach execution is complete and the queried data is returned.
// * We Don't Need To return. use $ tree as a super global variable.
// First, obtain all the classified data
Mysql_connect ('127. 0.0.1: 808080', 'root', '1234abcd ');
$ SQL = "select * From itcast_shop.it_category where 1 order by sort_order ";
$ Result = mysql_query ($ SQL );
While ($ ROW = mysql_fetch_assoc ($ result )){
$ List [] = $ row;
}

// Then, call the recursive lookup Function
Gettree ($ list, 0, 0 );

// Finally, traverse and display
Foreach ($ tree as $ row ){
Echo str_repeat ('& nbsp;', $ row ['deep ']);
Echo $ row ['cat _ name'];
Echo '<br> ';
}

Function gettree ($ arr, $ p_id, $ deep = 0 ){
Foreach ($ arr as $ row ){
If ($ row ['parent _ id'] = $ p_id ){
$ Row ['deep '] = $ deep;
$ Globals ['tree'] [] = $ row;
// Locate a qualified record and recursively look for it in a deeper level! Search and return to continue this foreach
Gettree ($ arr, $ row ['cat _ id'], $ deep + 1 );
}
}
}




Ii. Use PHP to print the disk file directory
<? PHP
Function dirtree ($ path, $ deep = 0 ){
$ Dir_handle = opendir ($ PATH );

While ($ file = readdir ($ dir_handle ))! = False ){
If ($ file = '.' | $ file = '..'){

} Else {
If (is_dir ($ path. '/'. $ file )){
Echo str_repeat ('& nbsp;', $ deep );
Echo '+', $ file, '<br/> ';
Dirtree ($ path. '/'. $ file, $ deep + 1 );
} Else {
Echo str_repeat ('& nbsp;', $ deep );
Echo $ file, '<br/> ';
}
}
}
}

$ Path = 'd: \ amp \ apache \ htdocs \ study ';
Dirtree ($ PATH );

Unlimited category and disk file directory Printing

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.