加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-14 13:57:24 所属栏目:大数据 来源:网络整理
导读:本文实例为大家分享了thinkphp实现无限分类的详细代码,希望对大家学习无限分类有所启发。 数据库:test数据表:(tp_category): Common/conf/config.php array( 'db_type' => 'mysql','db_user' => 'root','db_pwd' => '','db_host' => 'localhost','db_p

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

数据库:test 数据表:(tp_category):

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

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请求,递归显示所有分类):

方法 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

分类测试

另一种无限级分类:

JS递归(特殊):

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

');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请求数据:

// 分类
$('[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实现无限分类的方法,希望对大家的学习有所帮助。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读