本文实例为大家分享了thinkphp实现无限分类的详细代码,希望对大家学习无限分类有所启发。
数据库:test 数据表:(tp_category):
Common/conf/config.php
Common/function.php 遍历函数loop
Controller/IndexController.class.php
在模板(View/Index/test.html)中输出(仅支持2级分类,如果想全部显示,建议先把数组转换成JSON格式,然后通过AJAX请求,JS生成)
后续(ajax请求,递归显示所有分类):
方法 Controller/IndexController.class.php
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.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实现无限分类的方法,希望对大家的学习有所帮助。