标题:php+smarty循环数组怎么循环?
只看楼主
weilili
Rank: 1
等 级:新手上路
帖 子:45
专家分:0
注 册:2007-5-18
结帖率:100%
已结贴  问题点数:0 回复次数:3 
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}>

请高手帮忙!
搜索更多相关主题的帖子: smarty php 
2010-11-03 18:44
风月_无边
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:60
帖 子:2959
专家分:652
注 册:2007-1-19
得分:7 
里面再套一层循环 就是三层了

我的网站 http://www.
2010-11-03 21:31
weilili
Rank: 1
等 级:新手上路
帖 子:45
专家分:0
注 册:2007-5-18
得分:0 
这个$menu.childrens 什么东西都没有
2010-11-09 11:52



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-324874-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.496539 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved