Compared with the previous recursive functions, the MySQL contact function is very convenient to implement the unlimited classification of PHP.
-- Table structure for table 'category '-- Create Table 'category' ('id' int (11) not null auto_increment, 'catpath' varchar (255) default null, 'name' varchar (255) default null, primary key ('id') engine = MyISAM default charset = utf8 auto_increment = 11; -- dumping data for table 'category '-- insert into 'category' values (1, '0', 'website'); insert into 'category' values (2, '0-1', 'linux OS '); insert into 'category 'values (3, '0-1', 'apache Server '); insert into 'category 'values (4, '0-1', 'mysql database'); insert into 'category 'values (5, '0-1 ', 'php scripting language '); insert into 'category' values (6, '0-1-2', 'linux system example'); insert into 'category' values (7, '0-1-2 ', 'linux Network Technology'); insert into 'category 'values (8, '0-1-2 ', 'linux Security Basics '); insert into 'category 'values (9, '0-1-2-7 ', 'linux lamp'); insert into 'category 'values (10, '0-1-3-10 ', 'apache Server ');
The PHP code is as follows:
<? Phpheader ("Content-Type: text/html; charset = UTF-8");/*** generic tree class, you can generate any tree structure */$ conn = mysql_connect ('localhost', 'root', ''); mysql_select_db ('test', $ conn ); mysql_query ('set names utf8'); $ SQL = "select ID, Concat (catpath, '-', ID) as abspath, name from category order by abspath "; $ query = mysql_query ($ SQL); $ option = ''; while ($ ROW = mysql_fetch_array ($ query )) {/*** first display method $ Space = str_repeat ('', Coun T (explode ('-', $ row ['abspath'])-1); echo $ space. $ row ['name']. '<br>';/*** second Display Method */$ Space = str_repeat ('', count (explode ('-', $ row ['abspath'])-1); $ option. = '<option value = "'. $ row ['id']. '"> '. $ space. $ row ['name']. '</option>';} echo '<select name = "Opt"> '. $ option. '</SELECT>';?>