我的控制器是
[code]$data['navlist'] = $this->MCats->getCategoriesNav();[/code]
模型是
[code]
function getCategoriesNav(){
$data = array();
$this->db->group_by('parentid', 'id');
$Q = $this->db->get('categories');
if($Q->num_rows >0){
foreach($Q->result() as $row){
if($row->parentid > 0){
$data[0][$row->parentid]['children'][$row->id] = $row->name;
}else{
$data[0][$row->id]['name'] = $row->name;
}
}
}
$Q->free_result();
return $data;
}
[/code]
和观点是
[code]if(count($navlist)){
echo "<ul>";
foreach($navlist as $key => $list){
foreach($list as $topkey => $toplist){
echo "<li class='cat'>";
echo anchor("welcome/cat/$topkey", $toplist);
echo "</li>\n";
if(count($toplist['children'])){
foreach($toplist['children'] as $subkey => $subname){
echo "\n<li class='subcat'>";
echo anchor("welcome/cat/$subkey", $subname);
echo "</li>";
}
}
}
}
echo "</ul>\n";
}[/code]
我没有获得顶级菜单的类别名称,请帮助我,
答案 0 :(得分:0)
看起来你在两个不同地方使用的$ data [0]应该是$ data,而是将所有循环db结果加载到一个数组键中,键0