php无限极分类

代码:

 1 function judeg($conn,$id,$table){//用来判断有多少个父级,返回值用来循环本身有多少个分割符,$id传回来的值为当前数据的id值用来查找他的父级id
 2         $sql = "select chr_pid from ".$table." where chr_id = $id";
 3         $rs = mysqli_query($conn,$sql);
 4         if($rs){
 5             $row=mysqli_fetch_assoc($rs);
 6             $i=1;
 7             if($row['chr_pid']<>0){//判断他是否还有父级
 8                 $i=$i+judeg($conn,$row['chr_pid'],$table);
 9             }
10         }
11         return $i;
12     }
13     function cid($conn,$id,$table,$pid){//$pid传回来的为父级id,若没有则直接传回$pid;$table传回来的表名;$conn链接服务器代码,$id为传回的分类最顶级的值,在这里因为最顶部为全部分类所以传回的值为0(他的value值)
14         $sql = "select * from ".$table." where chr_pid = ".$id." order by chr_pid asc";
15         $rs = mysqli_query($conn,$sql);
16         $str = "";
17         if($rs){
18             while($row = mysqli_fetch_assoc($rs)){
19                 $title = $row['chr_title'];
20                 $p_id = $row['chr_id'];
21                 $cutOff = "";
22                 for($i=0;$i<judeg($conn,$p_id,$table);$i++){
23                     $cutOff.="-";
24                 }
25                 if($row['chr_id'] == $pid){//$pid 是用来修改时显示他本身的父级的,通过它的id来显示他
26 $str.='<option selected="selected" value="'.$p_id.'">'.$cutOff.$title.'</option>'; 27 }else{ 28 $str.='<option value="'.$p_id.'">'.$cutOff.$title.'</option>'; 29  } 30 $str.=cid($conn,$p_id,$table); 31  } 32  } 33 return $str; 34 }

  数据库表:

  

原文地址:https://www.cnblogs.com/CcPz/p/8565968.html