There are roughly three methods for unlimited php classification,
1. The database performs a unique index by setting the parent class ID, and then uses the recursive call of the function to implement unlimited classification;
2. The database is designed to be arranged in a specific format, and then use mysql to query the key function: concat. Program Implementation is relatively simple;
3. The third type is not too familiar. It seems that algorithms and data structures need to be used for sorting.
Today, I am mainly sharing the second method. At the beginning, I also found a lot of information, which is really hard to understand. But I finally figured it out, so I wrote down the article and hoped that this article could help you.
I. Database Design:
Copy codeThe Code is as follows :--
-- Table structure for table 'category'
--
Create table if not exists '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 '('id', 'catpath', 'name') VALUES
(1, '0', 'website '),
(2, '0-1', 'linux OS '),
(3, '0-1', 'apache Server '),
(4, '0-1', 'mysql database '),
(5, '0-1', 'php scripting language '),
(6, '0-1-2 ', 'linux system tutorial '),
(7, '0-1-2 ', 'linux network techno '),
(8, '0-1-2 ', 'linux Security Basics '),
(9, '0-1-2-7 ', 'linux LAMP '),
(10, '0-1-3-10 ', 'apache Server ');
Here, the-link symbol of catpath is not fixed and can be selected, and other special symbols.
Ii. PHP code implementation:
Copy codeThe Code is as follows: $ 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 );
While ($ row = mysql_fetch_array ($ query )){
/**
* Method 1
*/
/* $ Space = str_repeat ('', count (explode ('-', $ row ['abspath'])-1 );
Echo $ space. $ row ['name']. '<br> ';*/
/**
* Method 2
*/
$ Space = str_repeat ('', count (explode ('-', $ row ['abspath'])-1 );
$ Option. = '<option value = "'. $ row ['id']. '"> '. $ space. $ row ['name']. '</option> ';
}
Echo '<select name = "opt">'. $ option. '</select> ';
On:
Note the following key points:
1. The concat function is used to query fields in the database. If you are not familiar with this function, You can google it.
2. In the second place, str_repeat in php is used to cleverly set spaces.
There is an error, hope mail: chenghuiyong1987@gmail.com or leave a message