简单无限级分类

原创
2016-06-07 11:37:18 936浏览
数据层一个方法递归查询分类,返回多维数组。
控制器一个方法拼接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='
    ';//最外侧第一级
    }else{
    $treeHtml = '
      ';
      }
      foreach ($cates as $value) {
      $child='';
      if (count($value['child']) != 0) {
      $child=$this->cateTreeHtml($value['child'],$index+1);//递归
      }
      $treeHtml .= '';
      }
      $treeHtml .= '
    ';
    return $treeHtml;
    }
    //////////////////////////////////////////////////////////通过model层获取多维分类多维数组调用cateTreeHtml()方法
    //控制器层通过递归函数获取html
    $catesHtml=$this->cateTreeHtml($cates,$index=1);
    $postTags = $termModel->getPostTags($id);

    AD:真正免费,域名+虚机+企业邮箱=0元

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。