本文实例讲述了PHP实现多级分类生成树的方法。分享给大家供大家参考,具体如下:
条件,数据库里分类是按id,fid(父ID)实现多级分类的!
使用方法:
1
2
3
4
5
|
$sql = "XXXXXXXXXX" ; //sql语句 $res = $db ->Select( $sql ); //执行sql $list = array (); treeList(treeGet( $res ), $list ); /生成树 print_r( $res ); //打印出来看看! |
使用结果:
1
2
3
4
5
|
┣推荐新闻啊 ┃┣国际新闻 ┃┣dfffffg ┃┣ttttttt ┃┃┗yyyyy |
代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
/** * 选择SQL涵数 * * @access public * @param Array $field 字段信息,支持涵数 * @param Array $table 数据库表 * @param Array $where 条件 * @return SQL SQL语句 */ function treeGet( $data ) { $tmptree =null; $tree = $data ; return treeAddNodeToTree( $tmptree ,treegetbyuid( $tree ,0,@ $field ), $tree ); } /** *插入SQL涵数 * * @access public * @param Array $fieldResult 字段信息,支持涵数 * @param Array $table 数据库表 * @return SQL SQL语句 */ function treeAddNodeToTree( $Node , $miniTree ,& $source ) { if ( is_array ( $miniTree )) { foreach ( $miniTree as $k => $v ) { if (! count ( $miniTree [ $k ][ 'child' ]=treeAddNodeToTree( $miniTree [ $k ],treegetbyuid( $source ,@ $v [ 'id' ]), $source ))) { unset( $miniTree [ $k ][ 'child' ]); $miniTree [ $k ][ 'leaf' ]=true; //设置叶结点 } } return $Node [ 'child' ]= $miniTree ; } } function treegetbyuid(& $stree , $uid ) { $dtree = array (); if ( is_array ( $stree )){ foreach ( $stree as $k => $v ) { if ( $v [ 'fid' ]== $uid ) { $mytmp = array (); $mytmp = $v ; unset( $stree [ $k ]); array_push ( $dtree , $mytmp ); $mytmp =null; } } } return $dtree ; } /** *更新SQL涵数 * * @access public * @param Array $fieldResult 字段信息,支持涵数 * @param Array $table 数据库表 * @param Array $where 条件 * @return SQL SQL语句 */ function treeMakeDeep( $deep ) { $returnValue = "" ; for (; $deep ; $deep --) { $returnValue .= "┃" ; } return $returnValue . "┣" ; } function treeList( $treeData ,& $List ) { static $deep =0; if ( is_array ( $treeData )) { foreach ( $treeData as $k => $v ) { $v [ 'deepValue' ]=treeMakeDeep( $deep ); $v [ 'deep' ]= $deep ; $t = $v ; unset( $t [ 'child' ]); array_push ( $List , $t ); if ( $v [ 'child' ]) { ++ $deep ; $optionsNode .=treeList( $v [ 'child' ], $List ); $deep --; } } if ( $lastV = array_pop ( $List )) { $lastV [ 'deepValue' ]= str_replace ( '┣' , '┗' , $lastV [ 'deepValue' ]); array_push ( $List , $lastV ); } } } function treeSelect( $tree , $id , $options = "child" ) { switch ( strtolower ( $options )) { case "child" : $tmpTree = array (); $deep =-1; foreach ( $tree as $k => $v ) { if ( $id == $v [ 'id' ]) { array_push ( $tmpTree , $v ); $deep = $v [ 'deep' ]; } elseif ( $deep !=-1) { if ( $v [ 'deep' ]<= $deep ) { break ; } else { array_push ( $tmpTree , $v ); } } } break ; case "remove" : default : $tmpTree = array (); $deep =-1; foreach ( $tree as $k => $v ) { if ( $id == $v [ 'id' ]) { $deep = $v [ 'deep' ]; continue ; } elseif ( $deep !=-1) { if ( $v [ 'deep' ]<= $deep ) { array_push ( $tmpTree , $v ); $deep =-1; } continue ; } array_push ( $tmpTree , $v ); } } return $tmpTree ; } |
希望本文所述对大家PHP程序设计有所帮助。