大家帮我看看这段代码怎么取不到根分类的ID!急!
/*
* 根据子类ID获取根分类
*/
function get_root_class($classId) {
global $db, $lot_array;
$class = $db->fetch_first(“SELECT pid, name FROM new_infor_class WHERE id = $classId”);
if ($class[‘pid’] != 0) {
get_root_class($class[‘pid’]);
} else {
return $class[‘name’];
}
}
分类是按照无限分类设置,这样返回的就是$classId的分类名,实在不明白呀!哪位大哥救救我!
——解决方案——————–
好的,就搭一个测试环境
- SQL code
---- 表的结构 `tree`--CREATE TABLE IF NOT EXISTS `tree` ( `id` int(<i>1本文来#源gaodai$ma#com搞$代*码*网</i><pre>搞代gaodaima码
11) NOT NULL AUTO_INCREMENT, `pid` int(11) DEFAULT NULL, `name` varchar(10) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;—- 转存表中的数据 `tree`–INSERT INTO `tree` (`id`, `pid`, `name`) VALUES(1, 0, ‘a’),(2, 1, ‘b’),(3, 2, ‘c’),(4, 3, ‘d’);