PHP学习:Category类库无限分类
来源:文书网 1.54W
学习是没有尽头的,只有在不断的练习中才能提高自己。以下是本站小编精心为大家整理的关于PHP语言学习的Category类库 无限分类方面的知识,希望对大家有所帮助!更多内容请关注应届毕业生网!
以下是使用该类库的方法
123 | include ( "Common/" ); $Category = new Category( "ArticleCategory" , array ( 'id' , 'pid' , 'name' , 'fullname' )); $categoryList = $Category ->getList(); |
1、通过include包含类库
2、通过new实例化类
3、调用getList()方法获取所有分类列表
4、返回:所有分类列表,可以通过获取fullname显示参考。
效果如图:
以下是类库完整源码:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 | <?php
/**
*类功能:php无限分类
*author:
*使用方法见:
*/ class Category{
private $model ; //分类的数据表模型
private $rawList = array (); //原始的分类数据
private $formatList = array (); //格式化后的分类
private $error = "" ; //错误信息
private $icon = array ( ' │' , ' ├' , ' └' ); //格式化的字符
private $fields = array (); //字段映射,分类id,上级分类pid,分类名称name,格式化后分类名称fullname
/**
*构造函数,对象初始化
*@paramarray,object$model数组或对象,基于TP3.0的.数据表模型名称,若不采用TP,可传递空值。
*@paramarray$field字段映射,分类cid,上级分类pid,分类名称,格式化后分类名称fullname
*/
public function __construct( $model = '' , $fields = array ()){
if ( is_string ( $model )&&(! empty ( $model ))){
if (! $this ->model=D( $model ))
$this ->error= $model . "模型不存在!" ;
}
if ( is_object ( $model ))
$this ->model=& $model ;
$this ->fields[ 'cid' ]= $fields [ '0' ]? $fields [ '0' ]: 'id' ;
$this ->fields[ 'pid' ]= $fields [ '1' ]? $fields [ '1' ]: 'pid' ;
$this ->fields[ 'name' ]= $fields [ '2' ]? $fields [ '2' ]: 'name' ;
$this ->fields[ 'fullname' ]= $fields [ '3' ]? $fields [ '3' ]: 'fullname' ;
}
/**
*获取分类信息数据
*@paramarray,string$condition查询条件
*@paramstring$orderby排序
*/
private function _findAllCat( $condition , $orderby =NULL){
$this ->rawList= $this ->model->where( $condition )->order( $orderby )->select();
}
/**
*返回给定上级分类$pid的所有同一级子分类
*@paramint$pid传入要查询的pid
*@returnarray返回结构信息
*/
public function getChild( $pid ){
$childs = array ();
foreach ( $this ->rawList as $Category ){
if ( $Category [ $this ->fields[ 'pid' ]]== $pid ){
$childs []= $Category ;
}
}
return $childs ;
}
/**
*递归格式化分类前的字符
*@paramint$cid分类cid
*@paramstring$space
*/
private function _searchList( $cid =0, $space = "" ){
$childs = $this ->getChild( $cid );
//下级分类的数组
//如果没下级分类,结束递归
if (!( $n = count ( $childs ))){
return ;
}
$m =1;
//循环所有的下级分类
for ( $i =0; $i < $n ; $i ++){
$pre = "" ;
$pad = "" ;
if ( $n == $m ){
$pre = $this ->icon[2];
} else {
$pre = $this ->icon[1];
$pad = $space ? $this ->icon[0]: "" ;
}
$childs [ $i ][ $this ->fields[ 'fullname' ]]=( $space ? $space . $pre : "" ). $childs [ $i ][ $this ->fields[ 'name' ]];
$this ->formatList[]= $childs [ $i ];
$this ->_searchList( $childs [ $i ][ $this ->fields[ 'cid' ]], $space . $pad . " " ); //递归下一级分类
$m ++;
}
}
|