Tree.class代码:
<? php
/**
* [无限分级类]
* @Author xiaohao
* @version v1.0
* @time 2014-12-5
*/
// +----------------------------------------------------------------------
namespace Lib ;
class Tree {
static public function findChild (& $data , $pid = 0 , $col_pid = 'parent' ) {
$rootList = array ();
foreach ( $data as $key => $val ) {
if ( $val [ $col_pid ] == $pid ) {
$rootList [] = $val ;
unset ( $data [ $key ]);
}
}
return $rootList ;
}
/**
* 无限分级
* @access public
* @param array &$data 数据库里取得的结果集 地址引用
* @param integer $pid 父级id的值
* @param string $col_id 自增id字段名(对应&$data里的字段名)
* @param string $col_pid 父级字段名(对应&$data里的字段名)
* @param string $col_cid 是否存在子级字段名(对应&$data里的字段名)
* @return array $childs 返回整理好的数组
*/
static public function getTree (& $data , $pid = 0 , $col_id = 'id' , $col_pid = 'parent' , $col_cid = 'haschild' ) {
$childs = self :: findChild ( $data , $pid , $col_pid );
if ( empty ( $childs )) {
return null ;
}
foreach ( $childs as $key => $val ) {
if ( $val [ $col_cid ]) {
$treeList = self :: getTree ( $data , $val [ $col_id ], $col_id , $col_pid , $col_cid );
if ( $treeList !== null ) {
$childs [ $key ][ 'childs' ] = $treeList ;
}
}
}
return $childs ;
}
}
复制代码
控制器中调用:
$cateObj = M ( 'category' );
$cateRow = $cateObj -> order ( array ( 'cate_parent' => 'asc' ))-> select (); //查询数据时对parent父级排序
$treeObj = new \Lib\Tree (); //引用Tree类
$row = $treeObj -> getTree ( $cateRow , $pid = 0 , $col_id = 'cate_id' , $col_pid = 'cate_parent' , $col_cid = 'cate_haschild' ); //$col_id,$col_pid,$col_cid对应分类表category中的字段
$this -> assign ( 'cateRow' , $row );
$this -> display ( 'cateList' );
复制代码
view模板中调用:按各人的样式自行修改
<if condition = "!empty($cateRow)" >
<php>
function showTree($data) {
echo ' <ol class = "dd-list" > ';
foreach ($data as $key => $val) {
echo ' <li class = "dd-item item-orange " data-id = "'.$val[cate_id].'" > ';
echo ' <div class = "dd-handle" > '.$val['cate_name'];
echo ' <div class = "pull-right action-buttons" > ';
echo ' <a class = "green" data-toggle = "modal" href = "'.U('Admin/Goods/addCate/?parent='.$val[cate_id]).'" data-target = "#myModal" > ';
echo ' <i class = "ace-icon fa fa-plus-circle bigger-130" ></i></a> ';
echo ' <a class = "blue" data-toggle = "modal" href = "'.U('Admin/Goods/addCate/?cateid='.$val[cate_id]).'" data-target = "#myModal" ><i class = "ace-icon fa fa-pencil bigger-130" ></i></a><a class = "red" href = "#" ><i class = "ace-icon fa fa-trash-o bigger-130" ></i></a></div></div> ';
if (!empty($val['childs'])) {
showTree($val['childs']);
}
echo ' </li> ';
}
echo ' </ol> ';
}
showTree($cateRow);
</php>
</if>
复制代码
云栖大会北京站:阿里技术专家难得出镜,这次一下来了100多位?!