递归获取所有的子分类的ID:
//递归获取所有的子分类的ID function get_all_child($array,$id){ $arr = array(); foreach($array as $v){ if($v[‘pid‘] == $id){ $arr[] = $v[‘id‘]; $arr = array_merge($arr,get_all_child($array,$v[‘id‘])); }; }; return $arr; }
删除分类及其子分类:
//删除分类 public function delCate(){ $get = I(‘get.pid‘); $cateId = M(‘category‘)->field(array(‘id‘,‘pid‘))->select(); $delId = get_all_child($cateId,$get); $delId[] = $get; $where = array( ‘id‘ => array(‘IN‘,$delId) ); if(!M(‘category‘)->where($where)->delete()){ $this->error(‘删除失败‘); }; $this->success(‘删除成功‘,U(‘index‘)); }
时间: 2024-10-14 01:52:58