php怎样通过大分类的id查找全部小分类。在通过小分类的id查找小分类下的全部内容。。。求代码

2025-06-27 12:32:47
推荐回答(1个)
回答1:

楼主你好,本人解答这个问题分为三个步骤:

1.数据库表设计

    id int(6) not null auto_increment primary key

    name varchar(20) not null ,

    pid int(6),

    sort int (6)


2.php代码如下:


class category{

   Static Public function parents_to_child($data,$pid=0,$level=0,$html='--'){
       
       $arr = array();
       foreach($data as $v){
       
           if($v['pid'] == $pid){
               $v['level'] = $level+1;
               $v['html'] = str_repeat($html,$level);
               $arr[] = $v;
               $arr = array_merge($arr,self::parents_to_child($data,$pid=$v['id'],$level=$level+1));
           }
       
       }
       return $arr;
   }

}

?>

3.在你需要分类的文件内载入类category,并引用静态方法

    require 'category.class.php';
    
    $cate = category::parents_to_child($data);
?>

以下代码是返回一个一维数组的无限分类

楼主可以根据本人所提供的代码根据自己的需求修改