php tp无限分类,thinkphp实现无限分类(使用递归)

搜索热词

本文实例为大家分享了thinkPHP实现无限分类的详细代码,希望对大家学习无限分类有所启发。

数据库:test

数据表:(tp_category):

20151219112100807.png?20151119112117

Common/conf/config.PHP

array(

'db_type' => 'MysqL','db_user' => 'root','db_pwd' => '','db_host' => 'localhost','db_port' => '3306','db_name' => 'test','DB_PREFIX' => 'tp_',// 数据库表前缀

'DB_CHARSET'=> 'utf8',// 字符集

'DB_DEBUG' => TRUE,// 数据库调试模式 开启后可以记录sql日志 3.2.3新增

),

Common/function.PHP 遍历函数loop

PHP;">

/*

* 递归遍历

* @param $data array

* @param $id int

* return array

* */

function recursion($data,$id=0) {

$list = array();

foreach($data as $v) {

if($v['pid'] == $id) {

$v['son'] = recursion($data,$v['id']);

if(empty($v['son'])) {

unset($v['son']);

}

array_push($list,$v);

}

}

return $list;

}

Controller/IndexController.class.PHP

select();

$result = loop($category);

var_dump($result);

$this->assign('list',$result);

$this->display();

}

在模板(View/Index/test.html)中输出(仅支持2级分类,如果想全部显示,建议先把数组转换成JSON格式,然后通过AJAX请求,JS生成)

{$vo.category}

{$cate.category}

后续(ajax请求,递归显示所有分类):

20151219112132806.png?2015111911229

方法 Controller/IndexController.class.PHP

display();

}

public function resultCategory() {

$category = M('category',C('DB_CONFIG2'))->select();

$result = loop($category);

$this->ajaxReturn(array('data'=>$result,'status'=>'1','info'=>'获取列表成功'));

}

模板View/Index/test.html

Meta charset="UTF-8">

分类测试

另一种无限级分类:

20151219112313460.jpg?20151119112323

PHP;">

/**

* 无限极分类

* @param [type] $cate [description]

* @param integer $pid [description]

* @param integer $level [description]

* @param string $html [description]

* @return [type] [description]

*/

function sortOut($cate,$pid=0,$level=0,$html='--'){

$tree = array();

foreach($cate as $v){

if($v['pid'] == $pid){

$v['level'] = $level + 1;

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

$tree[] = $v;

$tree = array_merge($tree,sortOut($cate,$v['id'],$level+1,$html));

}

}

return $tree;

}

JS递归(特殊):

20151219112547641.jpg?2015111911276

这个函数相当于实现PHP的str_repeat函数

PHP;">

/* 字符串重复函数 */

if(!String.str_out_times) {

String.prototype.str_out_times = function(l) {

return new Array(l+1).join(this);

}

}

PHP;">

// 定位到当前选择

function recursion(selector,data,j,pid) {

var space = ' ┠ ';

if(!data) return false;

$.each(data,function(i,item) {

var opt = $('');selector.append(opt);

if(item.son && (item.son).length>0) {

recursion(selector,item.son,++j);

j=0;

}

});

// 当前是哪个分类

selector.find('option').each(function() {

if($(this).val() == pid) {

$(this).attr('selected','selected');

}

});

}

为什么j=0呢。因为执行顺序感觉与PHP不同,这里是从上到下加载。。

ajax请求数据:

PHP;">

$('.btn-edit').click(function() {

var id = $(this).data('id');

$.post("{:U('Article/editArticle')}",{id: id},function(res) {

// 分类

$('[name="pid"]').html('');

recursion($('[name="pid"]'),res.sort,res.pid);

$('[name="id"]').val(res.id);

$('[name="title"]').val(res.title);

$('[name="summary"]').val(res.summary);

$('#thumbnailImg').attr('src',"UPLOAD"+'/thumbnail/'+res.thumbnail);

ue.setContent(res.content);

$('#modal-edit').modal('show');

});

});

以上就是thinkPHP实现无限分类的方法,希望对大家的学习有所帮助。

总结

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值