@@ -4,11 +4,11 @@ class CategoryModel extends Model{
44 * 获取所有分类信息
55 * @return 所有分类
66 */
7- public function getList (){
7+ public function getList ($ p_id = 0 ){
88 $ sql = "select * from `demo_category` order by sort_order " ;
99 $ list = $ this ->db ->fetchAll ($ sql );
1010 //return $list;
11- $ list = $ this ->getTree ($ list ,0 ,0 );
11+ $ list = $ this ->getTree ($ list ,$ p_id ,0 );
1212 return $ list ;
1313 }
1414
@@ -19,7 +19,7 @@ public function getList(){
1919 * @param [type] $deep 当前的分类深度
2020 * @return [type] [description]
2121 */
22- public function getTree ($ arr ,$ pid =0 ,$ deep ){
22+ public function getTree ($ arr ,$ pid =0 ,$ deep= 0 ){
2323 static $ tree = array ();
2424 foreach ($ arr as $ row ) {
2525 if ($ row ['parent_id ' ] == $ pid ) {
@@ -30,4 +30,63 @@ public function getTree($arr,$pid=0,$deep){
3030 }
3131 return $ tree ;
3232 }
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+ }
3392}
0 commit comments