@@ -4,11 +4,11 @@ class CategoryModel extends Model{
4
4
* 获取所有分类信息
5
5
* @return 所有分类
6
6
*/
7
- public function getList (){
7
+ public function getList ($ p_id = 0 ){
8
8
$ sql = "select * from `demo_category` order by sort_order " ;
9
9
$ list = $ this ->db ->fetchAll ($ sql );
10
10
//return $list;
11
- $ list = $ this ->getTree ($ list ,0 ,0 );
11
+ $ list = $ this ->getTree ($ list ,$ p_id ,0 );
12
12
return $ list ;
13
13
}
14
14
@@ -19,7 +19,7 @@ public function getList(){
19
19
* @param [type] $deep 当前的分类深度
20
20
* @return [type] [description]
21
21
*/
22
- public function getTree ($ arr ,$ pid =0 ,$ deep ){
22
+ public function getTree ($ arr ,$ pid =0 ,$ deep= 0 ){
23
23
static $ tree = array ();
24
24
foreach ($ arr as $ row ) {
25
25
if ($ row ['parent_id ' ] == $ pid ) {
@@ -30,4 +30,63 @@ public function getTree($arr,$pid=0,$deep){
30
30
}
31
31
return $ tree ;
32
32
}
33
+ /**
34
+ * 利用ID删除分类
35
+ * @param [type] $id [description]
36
+ * @return bool
37
+ */
38
+ public function delById ($ id ){
39
+ if ($ this ->isLeaf ($ id ) ){
40
+ $ this ->error_info = '分类不是末级分类 ' ;
41
+ return false ;
42
+ }
43
+ $ sql = "delete from demo_category where `cat_id`= $ id " ;
44
+ return $ this ->db ->query ($ sql );
45
+ }
46
+
47
+ /**
48
+ * 判断当前分类下是否有子节点
49
+ * @return boolean [description]
50
+ */
51
+ public function isLeaf ($ cat_id ){
52
+ $ sql = "select count(*) from demo_category where parent_id= $ cat_id " ;
53
+ $ child_count = $ this ->db ->fetchColoum ($ sql );
54
+ return $ child_count == 0 ;
55
+ }
56
+
57
+ /**
58
+ * 插入新分类
59
+ * @param $data 当前插入分类的信息
60
+ */
61
+ public function addCate ($ data ){
62
+ if ($ data ['cat_name ' ] =='' ){
63
+ $ this ->error_info = '分类名不能为空 ' ;
64
+ // return false;
65
+ }
66
+
67
+ $ sql = "SELECT count(*) from demo_category where parent_id= {$ data ['parent_id ' ]} and cat_name=' {$ data ['cat_name ' ]}' " ;
68
+
69
+ $ cat_count = $ this ->db ->fetchColoum ($ sql );
70
+ if ($ cat_count > 0 ) {
71
+ $ this ->error_info = '分类已经存在 ' ;
72
+ }
73
+
74
+ $ sql = "INSERT INTO `demo_category`(`cat_name`, `sort_order`, `parent_id`) VALUES (' {$ data ['cat_name ' ]}',' {$ data ['sort_order ' ]}',' {$ data ['parent_id ' ]}') " ;
75
+ return $ this ->db ->query ($ sql );
76
+ }
77
+
78
+ public function getById ($ cat_id ){
79
+ $ sql = "select * from demo_category where cat_id=' {$ cat_id }' " ;
80
+ return $ this ->db ->fetchRow ($ sql );
81
+ }
82
+
83
+ public function pdateCat ($ data ){
84
+ $ child_list = $ this ->getList ($ data ['cat_id ' ]);
85
+ $ ids = array ($ data ['cat_id ' ]);
86
+ foreach ($ child_list as $ row ) {
87
+
88
+ }
89
+
90
+ $ sql = "update demo_category "
91
+ }
33
92
}
0 commit comments