现在的位置: 首页 -> PHP学习 -> PHP源码 -> thinkphp栏目中实现无极限分类

thinkphp栏目中实现无极限分类

2013-08-05 19:31评论数 2 ⁄ 被浏览 10591 views+

无极限分类应用广泛,作为一个开发者,想必会经常遇到,其实无极限分类用一个递归就可以实现,理解之后,也就只要5分钟就可以写出这个功能了。



//栏目管理

public function index(){

  $cat = M("Category");

  $catlist = $cat->where("catid>1")->select();

  $newArr = $this->getCategory($catlist);

  //dump($newArr);

  $this->assign("catlist",$newArr);

  $this->display();

}


// 递归调用栏目

public function getCategory($catlist,$asmenu='0',$html='——',$level='0'){

  $arr = array();

  foreach($catlist as $val){

    if($val['asmenu']==$asmenu){

      $val['html'] = str_repeat($html,$level);

      $arr[] = $val;

      $arr = array_merge($arr,self::getCategory($catlist,$val['catid'],$html,$level+1));

      //dump($arr);

      //exit();

    }  

  }

  return $arr;

}

 

文章出自:https://www.daixiaorui.com/read/20.html 本站所有文章,除注明出处外皆为原创,转载请注明本文地址,版权所有。

目前有 2 条评论  @我要评论

  1. 2楼IT共享导航 : 2016-10-07 20:47:47 评论说: @回复

    看不懂请百度IT共享导航

  2. 1楼xie : 2016-09-20 20:25:32 评论说: @回复

    能否解释一下呢?看不懂啊

您的每一个评论都是对我的一份支持

 博客二维码

昵称 *

邮箱 *