PHP - 递归实现无限极联动 发表于 2015-10-07 递归实现无限极联动123456789101112131415161718192021222324252627282930313233343536<?phpmysql_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(' ',$rows['deep']*20),$rows['cat_name'],'<br>';}?> 二级联动12345678910111213141516171819202122232425262728<?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> 三级联动123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960<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>