Today, I will share my thoughts on the infinite classification methods in php. For more information about php learning, see. 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:
The 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:
The 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'].'
';*/
/**
* Method 2
*/
$ Space = str_repeat ('', count (explode ('-', $ row ['abspath'])-1 );
$ Option. =''. $ Space. $ row ['name'].'';
}
Echo''. $ Option .'';
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