搜索
系统检测到您的用户名不符合规范:

简单无限级分类

浏览:3179 发布日期:2015年08月28日 分类:功能实现 关键字: 无限级分类
数据层一个方法递归查询分类,返回多维数组。
控制器一个方法拼接html搞定
效果图:
//没有排序哦~~
//数据库是wordpress的结构,如需了解数据结构,请自行百度wordpress数据库


/*-------------------------------------一下是model层文件-----------------------------------*/
//model层方法
public function getCateTree($parentId=0){
$Model=new Model();
$sql='select t1.*,t2.* from t_terms t1
left join t_term_taxonomy t2 on t1.term_id=t2.term_id
WHERE t2.taxonomy="category" AND t2.parent='.$parentId;
$parentCates=$Model->query($sql);
foreach($parentCates as $key=>$value){
$parentCates[$key]['child']=$this->getCateTree($value['term_id']);
}
return $parentCates;
}

/*---------------------------以下是控制器层文件------------------------------*/

//拼接html方法
/**
* $cates 分类多维数组
* $index 多维数组层次,默认$index=1即最顶层,之后每次+1
* return html
* */
public function cateTreeHtml($cates,$index)
{
if($index==1){
$treeHtml='<ul id="categorychecklist" data-wp-lists="list:category" class="categorychecklist form-no-clear">';//最外侧第一级
}else{
$treeHtml = '<ul class="children">';
}
foreach ($cates as $value) {
$child='';
if (count($value['child']) != 0) {
$child=$this->cateTreeHtml($value['child'],$index+1);//递归
}
$treeHtml .= '<li id="category-'.$value['term_id'].'" class="popular-category">
<label class="selectit">
<input value="'.$value['term_id'].'" type="checkbox" name="post_category[]" id="in-category-'.$value['term_id'].'">
'.$value['name'].'
</label>
'.$child//子分类
.'</li>';
}
$treeHtml .= '</ul>';
return $treeHtml;
}
//////////////////////////////////////////////////////////通过model层获取多维分类多维数组调用cateTreeHtml()方法
//控制器层通过递归函数获取html
$catesHtml=$this->cateTreeHtml($cates,$index=1);
$postTags = $termModel->getPostTags($id);
评论() 相关
后面还有条评论,
评论支持使用[code][/code]标签添加代码
您需要登录后才可以评论 登录 | 立即注册
收藏
Tonkong
积分:452 等级:LV2
热点推荐
(追記) (追記ここまで)
最新更新

我们

合作

网站

信息

ThinkPHP 是一个免费开源的,快速、简单的面向对象的 轻量级PHP开发框架 ,创立于2006年初,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。并且拥有众多的原创功能和特性,在社区团队的积极参与下,在易用性、扩展性和性能方面不断优化和改进,已经成长为国内最领先和最具影响力的WEB应用开发框架,众多的典型案例确保可以稳定用于商业以及门户级的开发。

AltStyle によって変換されたページ (->オリジナル) /