Thinkphp 获取所有子分类或父分类ID

 

/**
 * @Author:      HTL
 * @Email:       Huangyuan413026@163.com
 * @DateTime:    2016-04-22 11:25:02
 * @Description: 获取当前分类下所有子类ID
 * @pid:父类ID
 */
function get_child_ids($pid){
    return $this->__get_ids($pid,'','id');
}
 /**
 * @Author:      HTL
 * @Email:       Huangyuan413026@163.com
 * @DateTime:    2016-04-22 11:25:02
 * @Description: 获取当前分类下所有父类ID
 * @id:子类ID
 */
function get_parent_ids($id){
    return $this->__get_ids($id,'','pid');
}
 /**
 * @Author:      HTL
 * @Email:       Huangyuan413026@163.com
 * @DateTime:    2016-04-22 11:25:02
 * @Description: 获取类下所有父/子类ID
 * @pid:多个父/子类ID集以,分隔
 * @childids:找到的子/父分类列表
 * @find_column:where查找的字段[id|pid:default]
 */
function __get_ids($pid,$childids,$find_column = 'id'){
    if(!$pid || $pid<=0 || strlen(pid)<=0 || !in_array($find_column,array('id','pid'))) return 0;
    if(!$childids || strlen($childids)<=0) $childids = $pid;
    $column = ($find_column =='id'? "pid":"id");//id跟pid为互斥
    $ids = $this->model->where("$column in($pid)")->getField("$find_column",true);
    $ids = implode(",",$ids);
    
    //未找到,返回已经找到的
    if($ids<=0) return $childids;
    //添加到集合中
    $childids .= ','.$ids;
    //递归查找
    return $this->__get_ids($ids,$childids,$find_column);
}
 /**
 * @Author:      HTL
 * @Email:       Huangyuan413026@163.com
 * @DateTime:    2016-04-07 09:33:27
 * @Description: 默认状态更改
 */
function is_default(){
  $id = intval($_GET['id']);
	$type = intval($_GET['status']);
	if ($id>0) {
		//取消默认时将取消所有子分类的默认
		if($type!=1){
	    		$id = $this->_get_child_ids($id);
		}
    else{
        $id = $this->_get_parent_ids($id);
    }
    print_r($id);exit;
		$rst = $this->model->where("id in($id)")->setField('is_default',$type);
		if ($rst) {
			$this->success(L("SAVE_SUCCESS"), U("index"));
		} else {
			$this->error(L('SAVE_ERROR'));
		}
	} else {
		$this->error(L('Parameter_ERROR'));
	}
}

 

CREATE TABLE `thk_material` (
    `id` INT(11) NOT NULL AUTO_INCREMENT COMMENT '自增ID',
    `pid` INT(11) NULL DEFAULT '0' COMMENT '父类ID'   
);

 

 

 





posted @ 2016-07-13 14:37  HTL  阅读(5439)  评论(0编辑  收藏  举报
htl