PHP - 递归实现无限极联动

递归实现无限极联动

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
<?php
mysql_connect('localhost','root','aa');
mysql_select_db('php6');
mysql_query('set names utf8');
//获取所有类别
$sql='select * from category';
$rs=mysql_query($sql);
//将资源转成二维数组
$list=array();
while($rows=mysql_fetch_assoc($rs)) {
$list[]=$rows;
}
/**
*将二维数组按树形结构显示【递归】
*@param $list array 存放所有类别的二维数组
*@param $parentid int 在数组中查找属于某个父类的子级
*@param $deep int 元素的缩进深度
*/
function createTree($list,$parentid=0,$deep=0) {
static $tree=array(); //声明一个静态数组,这样在整个递归过程中一直存在
foreach($list as $rows) {
if($rows['parentid']==$parentid){
$rows['deep']=$deep;
$tree[]=$rows;
createTree($list,$rows['cat_id'],$deep+1);
}
}
return $tree;;
}
//测试
$array=createTree($list);
foreach($array as $rows) {
//str_repeat():字符串重复
echo str_repeat('&nbsp;',$rows['deep']*20),$rows['cat_name'],'<br>';
}
?>

二级联动

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
<?php
//连接数据库
mysql_connect('localhost','root','aa');
mysql_select_db('php6');
mysql_query('set names utf8');
//获取第一级
$sql="select * from category where parentid=0";
$rs1=mysql_query($sql);
?>
<select name="">
<?php
while($rows1=mysql_fetch_assoc($rs1)):
?>
<optgroup label="<?php echo $rows1['cat_name']?>">
<?php
$sql="select * from category where parentid=".$rows1['cat_id'];
$rs2=mysql_query($sql);
while($rows2=mysql_fetch_assoc($rs2)):
?>
<option value="<?php echo $rows2['cat_id']?>"><?php echo $rows2['cat_name']?></option>
<?php
endwhile;
?>
</optgroup>
<?php
endwhile;
?>
</select>

三级联动

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
<script type="text/javascript">
/*
*@param cat1 int 一级的id
*@param cat2 int 二级的id
*/
function jump(cat1,cat2) {
location.href='?cat1='+cat1+'&cat2='+cat2;
}
</script>
<?php
mysql_connect('localhost','root','aa');
mysql_select_db('php6');
mysql_query('set names utf8');

//获取一级和二级的编号
$cat1=isset($_GET['cat1'])?(int)$_GET['cat1']:''; //获取传递的一级编号
$cat2=isset($_GET['cat2'])?(int)$_GET['cat2']:''; //获取传递的二级编号
?>
<!--一级-->
<select id="cat1" onchange='jump(this.value,"")'>
<option value="">---请选择---</option>
<?php
$sql="select * from category where parentid=0";
$rs=mysql_query($sql);
while($rows=mysql_fetch_assoc($rs)):
?>
<option value="<?php echo $rows['cat_id']?>" <?php echo $cat1==$rows['cat_id']?'selected':''?>><?php echo $rows['cat_name']?></option>
<?php
endwhile;
?>
</select>
<!--二级-->
<select id="cat2" onchange='jump(document.getElementById("cat1").value,this.value)'>
<option value="">---请选择---</option>
<?php
if(isset($_GET['cat1'])):
$sql="select * from category where parentid=$cat1";
$rs=mysql_query($sql);
while($rows=mysql_fetch_assoc($rs)):
?>
<option value="<?php echo $rows['cat_id']?>" <?php echo $cat2==$rows['cat_id']?'selected':''?>><?php echo $rows['cat_name']?></option>
<?php
endwhile;
endif;
?>
</select>
<select id="cat3">
<option value="">---请选择---</option>
<?php
if(!empty($_GET['cat2'])):
$sql="select * from category where parentid=$cat2";
$rs=mysql_query($sql);
while($rows=mysql_fetch_assoc($rs)):
?>
<option value="<?php echo $rows['cat_id']?>" ><?php echo $rows['cat_name']?></option>
<?php
endwhile;
endif;
?>
</select>