手机版

在TP5数据库中四个字段实现无限分类的示例

时间:2021-08-19 来源:互联网 编辑:宝哥软件园 浏览:

效果:

CREATE TABLE ` new TABLE `(` id ' int(11)UNSIGNED NOT NULL AUTO _ INCREMENT,` PID ' int(10)UNSIGNED zero fill NOT NULL,` name ' varchar(30)CHARACTER SET utf8 COLLATE utf8 _ general _ ci NOT NULL,` path ' varchar(200)CHARACTER SET utf8 COLLATE utf8 _ general _ ci INCREMENT=53 checksum=0 row _ FORMAT=DYNAMICDELAY!DOCTYPE html html lang=' en ' head meta charset=' UTF-8 '标题文档/标题/标题正文h1四个字段的无限分类/h1表单操作="{ : URL('添加')} "方法="post "请选择上级分类:选择名称='pid '选项值='0 '顶级分类/option {foreach $cates as $vo}选项值={ $ VO。id } | { $ VO。pre } { $ VO。名称}/选项{/foreach }/选择br/请填写分类的名称:输入类型=' text ' name=' name '/br输入类型='submit '值='添加/表单/正文/html?phpnamespace应用程序\索引\控制器;使用思考\ Db使用思考\控制器;使用app \型号\类别;类索引扩展了控制器{公共函数索引(){ //添加分类的界面//查询所有的分类$ db=db :3360名称('类别');$data=$db-order('concat(path,'-',id)')-select();#order('concat(路径,'-',id)')自动排序foreach($ data as $ cate){ $ prefix=' ';$i=count(explode('-',$ cate[' path ']));for($ n=0;$ n=$ I;$ n){ $前缀。=' - ';} $ cate[' pre ']=$ prefix;} $this-assign('cates ',$ data);返回$ this-fetch();}公共函数add(){ $ db=db :名称('类别');if($ _ POST[' PID ']==0){ $ _ POST[' path ']=0;} else { $ _ POST[' path ']=$ db-where(' id=').$ _ POST[' PID '])-值('路径')。-'.$ _ POST[' PID '];} $ name=$ _ POST[' name '];if($ db-insert($ _ POST)){ $ this-success('添加成功');}else{ $this-error('添加失败');} #原理:4个字段/*标识号工艺流程图名称路径1 0服装0-1 2 0 游戏0-2 3 1 男装0-1-3 4 3 上衣0-1-3-4 */}}还有一个没有写完的递归无限分类

公共函数user(){ $ db=db :名称(' categro y1 ');//$ data=$ db-getTree();$ opt=$ db-GetOption($ data);$this-assign('opt ',$ opt);$this-assign('cates ',$ data);返回$ this-fetch();}公共函数doAdd(){ //执行添加数据$ db=db :3360名称(' categro y1 ');if($db-insert()){ $this-success('添加成功,网址('索引');}else{ $this-error('添加失败,网址('索引');}返回$ this-fetch();}型号

?phpclass CategoryModel扩展了模型{ #通过上级分类的主键编号号查询子类公共函数getTree($ PID=0){ $ data=$ this-where(' PID=').$ PID)-选择();foreach($数据为$cate){ #通过该分类的主键编号查询该分类的子类$ cate[' cates ']=$ this-getTree($ cate[' id ']);}返回$ data} #拼装属性结构#通过一个数组组装选择权公共函数getOption($ data){ static $ I=0;for($ n=0;$ n=$ I * 2;$ n){ $前缀。='-';} foreach($数据为$cate) { $opt .='选项值=' '。$cate['id']' '。$前缀$ cate[name ]./option ';#判断该分类下是否包含子类,如果有子类,将数组传入接着拼装if(!空($ case[' cates ']){ $ I;$opt .=$ this-GetOption($ case[' cases ']);} else { $ I=0;}返回$ opt} }}以上这篇在TP5数据库中四个字段实现无限分类的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

版权声明:在TP5数据库中四个字段实现无限分类的示例是由宝哥软件园云端程序自动收集整理而来。如果本文侵犯了你的权益,请联系本站底部QQ或者邮箱删除。