php+smarty循环数组怎么循环?
大家帮我个忙,关于菜单分类的问题,老板催了几天没能解决的,我只做出来二级类,三级类出不来函数代码是:
public function array_to_tree($arr, $fid = 'id', $fparent = 'parent_id',$fchildrens = 'childrens'){
$pkvRefs = array();
foreach ($arr as $offset => $row)$pkvRefs[$row[$fid]] =&$arr[$offset];
$tree = array();
foreach ($arr as $offset => $row) {
$parentId = $row[$fparent];
if ($parentId) {
if (!isset($pkvRefs[$parentId])) { continue; }
$parent =&$pkvRefs[$parentId];
$parent[$fchildrens][] =&$arr[$offset];
} else {
$tree[] =&$arr[$offset];
}
}
return $tree;
}
public function getCategoryRecall($table,$pid=0){
static $cats = array();
$sql = "select * from $table where pid=$pid order by pid asc";
$arr = $this->db->query($sql);
for($i=0,$count=count($arr);$i<$count;$i++){
$cats[]=$arr[$i];
$this->getCategoryRecall($table,$arr[$i]['id']);
}
return $cats;
}
调用时:
$menu=$this->model->getCategoryRecall('xy_navigation',3);
$menus=$this->model->array_to_tree($menu,$menu[0]['id'],$menu[0]['pid']);
smarty显示:
<{foreach from=$menu item=menu}>
<li><a href="index.php?a=team&c=index&pid=<{$menu.id}>" title="" ><{$menu.title}></a>
<{if $menu.childrens}>
<ul>
<{foreach from=$menu.childrens item=menus }>
<li><a href="index.php?a=team&c=index&pid=<{$menus.id}>" title="" ><{$menus.title}></a></li>
<{/foreach}>
</ul>
<{/if}>
</li>
<{/foreach}>
请高手帮忙!