PHP unlimited classification: three methods of non-function recursive calling! _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP unlimited classification: three methods of non-function recursive calling !. There are roughly three methods for php unlimited 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. there are roughly three methods for database design to use a specific php unlimited 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

Databases, 1. the database performs unique indexes by setting the parent class ID, and then uses the recursive call of the function to implement unlimited classification. 2. the database is designed through a specific...

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.