php 查找父類全部的子類

1.數據庫表設計php

    id int(6) not null auto_increment primary keyhtml

    name varchar(20) not null ,數據庫

    pid int(6),數組

    sort int (6)ui

 

2.php代碼以下:spa

<?php設計

 

class category{code

 

   Static Public function parents_to_child($data,$pid=0,$level=0,$html='--'){htm

        

       $arr array();ci

       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,並引用靜態方法

<?php

    require 'category.class.php';

     

    $cate = category::parents_to_child($data);

?>

如下代碼是返回一個一維數組的無限分類

樓主能夠根據本人所提供的代碼根據本身的需求修改

相關文章
相關標籤/搜索