/ onethink显示指定分类的同级分类或子分类列表 /
public function cates(){
$field = 'id,name,pid,title,link_id';
$category = M('Category')->where('status = 1')->order('sort asc')->select();
$count = M('Document')->where('type = 1')->group('category_id')->getField('category_id, count(*) as num');
foreach ($category as $key => $value) {
$category[$key]['article_num'] = (int)$count[$value['id']];
}
$this->assign('category', D('Category')->toFormatTree($category));
$this->display('Common/lists');
}
/*
将格式数组转换为树

@param array $list
@param integer $level 进行递归时传递用的参数
/
private $formatTree; //用于树型数组完成递归格式的全局变量
private function _toFormatTree($list,$level=0,$title = 'title') {
foreach($list as $key=>$val){
$tmp_str=str_repeat(" ",$level*2);
$tmp_str.="└";

$val['level'] = $level;
$val['title_show'] =$level==0?$val[$title]." ":$tmp_str.$val[$title]." ";
// $val['title_show'] = $val['id'].'|'.$level.'级|'.$val['title_show'];
if(!array_key_exists('_child',$val)){
array_push($this->formatTree,$val);
}else{
$tmp_ary = $val['_child'];
unset($val['_child']);
array_push($this->formatTree,$val);
   $this->_toFormatTree($tmp_ary,$level+1,$title); //进行下一层递归
}
}
return;
}

public function toFormatTree($list,$title = 'title',$pk='id',$pid = 'pid',$root = 0){
$list = list_to_tree($list,$pk,$pid,'_child',$root);
$this->formatTree = array();
$this->_toFormatTree($list,0,$title);
return $this->formatTree;
}
}

点赞(1) 打赏

评论列表 共有 0 条评论

暂无评论

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部