public function deleteCategory() { /*$id=$this->_get('id'); $category=M('category')->select(); $delete_category_id=getChildCategoryById($category,$id); //p($category);die; if(M('category')->where(array('id'=>array('IN',$delete_category_id)))->delete()){ $this->success('删除成功'); }else{ $this->error('删除失败'); }*/ $category_id = $this->_get('category_id'); $category = M('category')->select(); $delete_category_id = getChildCategoryById($category, $category_id); $delete_category_id[] = $category_id; //p($delete_category_id);die; $post = M('post')->where(array('category_id' => array('IN', $delete_category_id)))->select(); //p($post);die; if ($post) { $this->error('有文章属于该分类,请先删除该分类下全部文章才能删除该分类。。。'); } if (M('category')->where(array('category_id' => array('IN', $delete_category_id)))->delete()) { $this->success('删除成功'); } else { $this->error('删除失败'); } }
function getChildCategoryById($category, $category_id) { static $category_children_id = array(); if ($category) { foreach ($category as $key => $value) { if ($value['parent_id'] == $category_id) { $category_children_id[] = $value['category_id']; getChildCategoryById($category, $value['category_id']); } } } return $category_children_id; }