Home  >  Article  >  Backend Development  >  树形展示分类

树形展示分类

WBOY
WBOYOriginal
2016-06-13 11:15:501165browse

树形显示分类

本帖最后由 bing15 于 2013-02-02 14:07:19 编辑 从数据库中获取数据,以树形显示分类,如:
 公告 
 文章
  -新文章
  -热文章

分类数据库的结构是:
id,reid,topid,title

id:分类id
reid:父类id
topid:顶级id
title:名称
我先给出一点数据吧,这样做起来方便些。
id reid topid title
1   0    0     公告
2   0    0     文章
3   2    2    新文章
4   2    2    热文章

记住:可能新文章里面还有小分类哦,无限级分类。

------解决方案--------------------
<?<br />$arr = array(<br />     <br />    array('id'=>1,'city_name'=>'中国','rel_id'=>'1','pid'=>0),<br />    array('id'=>2,'city_name'=>'广东','rel_id'=>'1-2','pid'=>1),<br />    array('id'=>3,'city_name'=>'深圳','rel_id'=>'1-2-3','pid'=>2),<br />    array('id'=>4,'city_name'=>'广州','rel_id'=>'1-2-4','pid'=>2)<br />    <br />);<br /> <br />function find_subclass( $pid ){<br />     <br />    global $arr;<br />    $__arr = array();<br />    foreach ( $arr as $k=>$v )<br />    {<br />         <br />        if( $v['pid']==$pid )$__arr[] = $v;<br />         <br />    }<br />    return $__arr;<br />     <br />}<br /> <br />function tree_subclass($pid=0){<br />     <br />    $__arr = array();<br />    $__arr = find_subclass($pid);<br />    if( !empty($__arr) ){<br />         <br />        foreach ( $__arr as $k=>$v )<br />        {<br /> <br />            $__arr[$k]['subclass'] = tree_subclass($v['id']);<br />             <br />        }<br />         <br />    }<br />    return $__arr;<br />}<br /> <br />var_dump(tree_subclass(0));<br /> <br />?>

------解决方案--------------------
引用:
我的数据怎么递归显示分类下的子分类呢
见http://bbs.csdn.net/topics/390364669
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn