Find a small functional notation
There is a classification table CALSS
ID-----Unique Identity
PID-----Parent ID
Name-----Names
Path-----Paths
CREATE TABLE ' CALSS ' (
' id ' int (one) not NULL auto_increment,
' PID ' int (one) not NULL DEFAULT ' 0 ',
' Name ' varchar (+) is not NULL,
' Path ' varchar (+) not NULL,
PRIMARY KEY (' id ')
) Engine=innodb auto_increment=10 DEFAULT Charset=utf8;
Now ask for a path to be spelled
For example data as follows
INSERT into Calss VALUES (' 1 ', ' 0 ', ' Web development ', web);
INSERT into Calss VALUES (' 2 ', ' 1 ', ' backend ', control);
INSERT into Calss VALUES (' 3 ', ' 2 ', ' language ', 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 development ', phone);
INSERT into Calss VALUES (' 9 ', ' 8 ', ' iOS ', iOS);
Find the path to the PHP category
The final result is the following format
$php _path= "web/control/language/php/";
For a specific PHP code implementation is said to use recursive thought I saw a day recursion is not see understand the idea of recursion
Can not be implemented recursively, with recursive implementation please help to write clearly each code comments grateful.
The last sentence, boss, hello, girl, hello.
------Solution--------------------
Can follow what they say loop SQL gets
can also ... You can read it and change it.
PHP Code
SELECT Id,pid,path from Calss; Gets the dataset combined 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 = ' Langu Age ', 4 = ' data ', 5 = ' php ', 6 = ' jsp ', 7 = ' asp ', 8 = ' phone ', 9 = ' iOS ');//Get ID list $idarry=array () ; Gettopid (5, $array);//loop ID list Output final result 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/** * get ancestor 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 results based on superior 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;}