OneThink后端
您现在的位置是:博客主页 > OneThink > OneThink后端

点想网onethink博客分类即文章统计输出源代码

By 点想博主 on OneThink后端 Time 2015-07-17 from:互联网 2931人已围观
简介:    /* onethink显示指定分类的同级分类或子分类列表 */ public function cates(){ $field = 'id,name,pid,title,link_id'; $category = M('Category')->where('status = 1')->order('sort asc')->select
/* 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;
}
}
相关文章
文章评论

( 0 )