php+mysql无限级分类(非递归)
要实现无限级分类,递归一般是第一个也是最容易想到的,但是递归一般被认为占用资源的方法,所以很多系统是不考虑使用递归的
本文还是通过数据库的设计,用一句sql语句实现
数据库字段大概如下:
———————————————————————————–
id 编号
fid 父分类编号
class_name 分类名
path 分类路径,以 id 为节点,组成类似 ,1,2,3,4, 这样的字符串
———————————————————————————-

可以假设有如下的数据

id fid class_name path
—————————————————-
1? 0?????? 分类1 ,?????? 1,
2? 0 ????? 分类2 ,?????? 2,
3? 1?????? 分类1-1 ,??? 1,3,
4? 1?????? 分类1-2 ,??? 1,4,
5? 2?????? 分类2-1 ,??? 2,5,
6? 4?????? 分类1-2-1 ,?1,4,6,
—————————————————-
<?php?????
$sql=”SELECT * FROM tree order by path”;???
$result=$nbs->Query($sql);???
while($rows=$nbs->fetch_array($result)){???
??? if(substr_count($rows[‘path’],’,’)>2){???
??????? for($i=0;$i<(substr_count($rows[‘path’],’,’)-2);$i++)???
??????????? echo ‘ ‘;???
??? }???
??? echo $rows[‘class_name’].'<br>’;???
}???
?>???

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<span style="color: #000088">$conn</span> <span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/mysql_connect" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">mysql_connect</span></a> <span style="color: #009900">(</span> <span style="color: #0000ff">'localhost'</span><span style="color: #339933">,</span> <span style="color: #0000ff">'root'</span><span style="color: #339933">,</span> <span style="color: #0000ff">'root'</span> <span style="color: #009900">)</span><span style="color: #339933">;</span>
<a rel="noopener noreferrer nofollow" href="http://www.php.net/mysql_select_db" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">mysql_select_db</span></a> <span style="color: #009900">(</span> <span style="color: #0000ff">'wanggou123'</span><span style="color: #339933">,</span> <span style="color: #000088">$conn</span> <span style="color: #009900">)</span><span style="color: #339933">;</span>
<a rel="noopener noreferrer nofollow" href="http://www.php.net/mysql_query" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">mysql_query</span></a> <span style="color: #009900">(</span> <span style="color: #0000ff">'set names UTF8'</span> <span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000088">$sql</span> <span style="color: #339933">=</span> <span style="color: #0000ff">"select id,concat(catpath,'-',id) as abspath,name from category order by abspath"</span><span style="color: #339933">;</span>
 
<span style="color: #000088">$query</span> <span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/mysql_query" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">mysql_query</span></a> <span style="color: #009900">(</span> <span style="color: #000088">$sql</span> <span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #b1b100">while</span> <span style="color: #009900">(</span> <span style="color: #000088">$row</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/mysql_fetch_array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">mysql_fetch_array</span></a><span style="color: #009900">(</span><span style="color: #000088">$query</span><span style="color: #009900">)</span><span style="color: #009900">)</span> <span style="color: #009900">{</span>
    <span style="color: #009933;font-style: italic">/**
   * 第一种展示方法
*/</span>
<span style="color: #666666;font-style: italic">/*$space = str_repeat ( '&nbsp;&nbsp;&nbsp;&nbsp;', count ( explode ( '-', $row ['abspath'] ) ) - 1 );
echo $space . $row ['name'] . '
';*/</span>
<span style="color: #009933;font-style: italic">/**
 第二种展示方法
*/</span>
<span style="color: #000088">$space</span> <span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/str_repeat" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">str_repeat</span></a> <span style="color: #009900">(</span> <span style="color: #0000ff">'——'</span><span style="color: #339933">,</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/count" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">count</span></a> <span style="color: #009900">(</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/explode" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">explode</span></a> <span style="color: #009900">(</span> <span style="color: #0000ff">'-'</span><span style="color: #339933">,</span> <span style="color: #000088">$row</span> <span style="color: #009900">[</span><span style="color: #0000ff">'abspath'</span><span style="color: #009900">]</span> <span style="color: #009900">)</span> <span style="color: #009900">)</span> <span style="color: #339933">-</span> <span style="color: #cc66cc">1</span> <span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000088">$option</span> <span style="color: #339933">.=</span> <span style="color: #0000ff">''</span> <span style="color: #339933">.</span> <span style="color: #000088">$space</span> <span style="color: #339933">.</span> <span style="color: #000088">$row</span> <span style="color: #009900">[</span><span style="color: #0000ff">'name'</span><span style="color: #009900">]</span> <span style="color: #339933">.</span> <span style="color: #0000ff">'<Br>'</span><span style="color: #339933">;</span>
<span style="color: #009900">}</span>
<span style="color: #b1b100">echo</span> <span style="color: #000088">$option</span><span style="color: #339933">;</span>
<a rel="noopener noreferrer nofollow" href="http://www.php.net/exit" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">exit</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #b1b100">echo</span> <span style="color: #0000ff">'<select name="opt">'</span> <span style="color: #339933">.</span> <span style="color: #000088">$option</span> <span style="color: #339933">.</span> <span style="color: #0000ff">'</select>'</span><span style="color: #339933">;</span>

其中$nbs是数据库操作类,此方法简单明了!