Find a solution for small functional writing

Source: Internet
Author: User
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;}
  • 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.