注册 登录
编程论坛 PHP技术论坛

php+smarty循环数组怎么循环?

weilili 发布于 2010-11-03 18:44, 865 次点击
大家帮我个忙,关于菜单分类的问题,老板催了几天没能解决的,我只做出来二级类,三级类出不来
函数代码是:
    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}>

请高手帮忙!
3 回复
#2
风月_无边2010-11-03 21:31
里面再套一层循环 就是三层了
#3
weilili2010-11-09 11:52
这个$menu.childrens 什么东西都没有
1