效果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
CREATE TABLE `NewTable` ( `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT , `pid` int(10) UNSIGNED ZEROFILL NOT NULL , `name` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL , `path` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL , PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci AUTO_INCREMENT=53 CHECKSUM=0 ROW_FORMAT=DYNAMIC DELAY_KEY_WRITE=0 ; |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <title>Document</title> </head> <body> <h1>四个字段的无限分类</h1> <form action= "{:url('add')}" method= "post" > 请选择上级分类:<select name= "pid" > <option value= "0" >顶级分类</option> { foreach $cates as $vo } <option value={ $vo .id}>|{ $vo .pre}{ $vo .name}</option> {/ foreach } </select><br/> 请填写分类的名称:<input type= "text" name= "name" /></br> <input type= "submit" value= "添加" > </form> </body> </html> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
<?php namespace app\index\controller; use think\Db; use think\Controller; use app\model\Category; class Index extends Controller { public function index() { //添加分类的界面 //查询所有的分类 $db =Db::name( 'categroy' ); $data = $db ->order( 'concat(path,"-",id)' )->select(); #order( 'concat(path,"-",id)' )自动排序 foreach ( $data as & $cate ) { $prefix = '' ; $i = count ( explode ( "-" , $cate [ 'path' ])); for ( $n =0; $n <= $i ; $n ++){ $prefix .= "--" ; } $cate [ 'pre' ]= $prefix ; } $this ->assign( 'cates' , $data ); return $this ->fetch(); } public function add(){ $db =Db::name( 'categroy' ); if ( $_POST [ 'pid' ]==0){ $_POST [ 'path' ]=0; } else { $_POST [ 'path' ]= $db ->where( 'id=' . $_POST [ 'pid' ])->value( 'path' ). "-" . $_POST [ 'pid' ]; } $name = $_POST [ 'name' ]; if ( $db ->insert( $_POST )){ $this ->success( '添加成功' ); } else { $this ->error( '添加失败' ); } #原理:4个字段 /* ID PID NAME PATH 1 0 服装 0-1 2 0 游戏 0-2 3 1 男装 0-1-3 4 3 上衣 0-1-3-4 */ }} |
还有一个没有写完的递归无限分类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public function user(){ $db =Db::name( 'categroy1' ); // $data=$db->getTree(); $opt = $db ->getOption( $data ); $this ->assign( 'opt' , $opt ); $this ->assign( 'cates' , $data ); return $this ->fetch(); } public function doAdd(){ //执行添加数据 $db =Db::name( 'categroy1' ); if ( $db ->insert()){ $this ->success( '添加成功' ,url( 'index' )); } else { $this ->error( '添加失败' ,url( 'index' )); } return $this ->fetch(); } |
Model
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
<?php class CategoryModel extends Model{ #通过上级分类的主键id号查询子类 public function getTree( $pid =0){ $data = $this ->where( 'pid=' . $pid )->select(); foreach ( $data as & $cate ){ #通过该分类的主键id查询该分类的子类 $cate [ 'cates' ]= $this ->getTree( $cate [ 'id' ]); } return $data ; } #拼装属性结构 #通过一个数组组装option public function getOption( $data ){ static $i =0; for ( $n =0; $n <= $i *2; $n ++){ $prefix .= "-" ; } foreach ( $data as $cate ) { $opt .= "<option value='" . $cate ['id ']."' >". $prefix . $cate [ 'name' ]. "</option>" ; #判断该分类下是否包含子类,如果有子类,将数组传入接着拼装 if (! empty ( $case [ 'cates' ])){ $i ++; $opt .= $this ->getOption( $case [ 'cases' ]); } else { $i =0; } return $opt ; } } } |
以上这篇在TP5数据库中四个字段实现无限分类的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_36171533/article/details/79307643