有一个分类表 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;}
