Home >Backend Development >PHP Tutorial >求个小功能写法解决方案

求个小功能写法解决方案

WBOY
WBOYOriginal
2016-06-13 10:09:38927browse

求个小功能写法
有一个分类表 calss

ID -----唯一标识
pid -----父级ID
name -----名字
path -----路径

CREATE TABLE `calss` (
  `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;



现在求一个路径的写法

例如数据如下 

INSERT INTO calss VALUES ('1', '0', 'WEB开发',web);
INSERT INTO calss VALUES ('2', '1', '后端',control);
INSERT INTO calss VALUES ('3', '2', '语言',language);
INSERT INTO calss VALUES ('4', '2', '数据库',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', '手机应用开发',phone);
INSERT INTO calss VALUES ('9', '8', 'ios',ios);

求 php类别的 路径  
最终结果如下格式
$php_path="web/control/language/php/";


求个具体php代码实现 感激不尽 据说用到递归思想 我看了一天递归 楞是没看明白 递归的思想 

可否能不用递归实现,用递归实现的 请帮忙写清楚每一句代码注释 感激不尽了 


最后一句 老大 你好 妞哥 你好

------解决方案--------------------
可以按照他们说的循环SQL获取

也可以...随便写的,能看懂了自己再改哈

PHP code
//SELECT id,pid,path FROM calss;取得数据集组合成下面的数组$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');//取得id列表$idArry=array();getTopID(5, $array);//循环id列表输出最后结果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);//函数/** * 取得上级Id数组 * @param $id * @param $array * @return array */function getTopID($id, $array){    global $idArr;    if (isset($array[$id])) {        $idArr[] = $array[$id];        getTopID($array[$id], $array);    }}/** * 根据上级ID数组排序结果 * @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;}<div class="clear">
                 
              
              
        
            </div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn