Solutions for writing small functions

Source: Internet
Author: User
For a small function, there is a classification table calssID ----- unique identifier pid ----- parent IDname ----- name path ----- path CREATETABLE 'calss' (& nbsp; 'id' int (11) NOTNULLAUTO_INCREMENT, write a small function
There is a classification table calss

ID ----- unique ID
Pid ----- parent ID
Name ----- name
Path ----- path

Create table 'calls '(
'Id' int (11) not null AUTO_INCREMENT,
'Pid 'int (11) not null default '0 ',
'Name' varchar (32) not null,
'Path' varchar (32) not null,
Primary key ('id ')
) ENGINE = InnoDB AUTO_INCREMENT = 10 default charset = utf8;



How to write a path

For example, the data is as follows:

Insert into calss VALUES ('1', '0', 'Web developer', WEB );
Insert into calss VALUES ('2', '1', 'backbackend ', control );
Insert into calss VALUES ('3', '2', 'linguistic ', language );
Insert into calss VALUES ('4', '2', 'database', data );
Insert into calss VALUES ('5', '3', 'php', php );
Insert into calss VALUES ('6', '3', 'JSP ', jsp );
Insert into calss VALUES ('7', '3', 'asp ', asp );
Insert into calss VALUES ('8', '0', 'mobile app developing', phone );
Insert into calss VALUES ('9', '8', 'iOS ', ios );

Find the path of the php category
The final result is in the following format:
$ Php_path = "web/control/language/php /";


I am grateful for trying to implement a specific php code. it is said that the recursive idea is used. I have read the recursion idea for a day.

Can it be implemented without recursion? please help me understand every sentence of code comments with recursion. thank you very much.


Last sentence, Hello, girl, Hello

------ Solution --------------------
It can be obtained using the cyclic SQL statement they described.

You can also write it at will. you can understand it and change it again.
PHP code
// SELECT id, pid, path FROM calss; obtain the dataset and combine it into the following array $ array = array ('1' => 0, '2' => 1, '3' => 2, '4' => 2, '5' => 3, '6' => 3, '7' => 3, '8' => 0, '9' => 8); $ pathArray = array (1 => 'web', 2 => 'control ', 3 => 'language', 4 => 'Data', 5 => 'php', 6 => 'JSP ', 7 => 'asp ', 8 => 'phone', 9 => 'iOS '); // obtain the id list $ idArry = array (); getTopID (5, $ array ); // echo listPath (5, $ idArr, $ pathArray), "\ n"; unset ($ idArr); getTopID (6, $ array ); echo listpath (6, $ idArr, $ pathArray), "\ n"; unset ($ idArr); getTopID (8, $ array); echo listpath (8, $ idArr, $ pathArray), "\ n"; unset ($ idArr); getTopID (9, $ array); echo listpath (9, $ idArr, $ pathArray), "\ n "; unset ($ idArr); // function/*** obtain the upper-level Id array * @ param $ id * @ param $ array * @ return array */function getTopID ($ id, $ array) {global $ idArr; if (isset ($ array [$ id]) {$ idArr [] = $ array [$ id]; getTopID ($ array [$ id], $ array );}} /*** sort the result by the parent ID array * @ param $ id * @ param $ list * @ param $ array * @ return string */function listPath ($ id, $ list, $ array) {$ path = ''; array_push ($ list, $ id); sort ($ list); foreach ($ list as $ value) {if (isset ($ array [$ value]) {$ path. = $ array [$ value]. '/';} return $ path ;}

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.