phpjinggege
/**
 * 检测该分类下所有子分类,并输出ID(包括自己)
 * 数据库字段 catid pid
 */
function getChildrenIds ($sort_id){
	include_once APPPATH.\'/libraries/db.php\';
	$db = new Db();
	$ids = $sort_id;
	$sql = "SELECT catid,pid FROM jy_category WHERE pid =".$sort_id;
	$result = $db->select($sql);

	if ($result){
		foreach ($result as $key=>$val){
			$ids .= \',\'.$val[\'catid\'].",";
			$ids .= getChildrenIds ($val[\'catid\']);
		}
	}
	
	$idsArr = explode(",",$ids);
	$idsArr = array_unique($idsArr);
	$ids = implode(",",$idsArr);
	return $ids;
}

  

/**
 * 检测该分类下所有子分类,并输出ID(不包括自己)
 * 数据库字段 catid pid
 */
function getChildrenIds ($sort_id)
   {
       $db = $this->loadDB();
       $ids = \'\';
       $sql = "SELECT * FROM t_dept WHERE `parent_id` = \'{$sort_id}\'";
       $query = $db->query($sql);
       $result = $query->result_array();
 
       if ($result)
       {
           foreach ($result as $key=>$val)
           {
               $ids .= \',\'.$val[\'id\'];
               $ids .= $this->getChildrenIds ($val[\'id\']);
           }
       }
       return $ids;
}

  上面的查询数据库次数太多了 然后下面这个就好一点,三级分类最多查询三次

	/**
	 * 检测该分类下所有子分类,并输出ID
	 */
	public function getChildrenIds ($pid,$sort_id){
		$aPid = array();
		if(!is_array($pid)){
			$aPid[]=$pid;
		}else{
			$aPid = $pid;
		}
		
		$sql = "SELECT catid,pid FROM jy_category WHERE pid in({$sort_id})";
		$result =$this->db->select($sql);

		$aSort = array();
		$aUnion = array();
		if ($result){
			foreach ($result as $key=>$val){
				$aSort[] = $val[\'catid\'];
			}

			$aUnion = $this->getChildrenIds($aSort,implode(",",$aSort));
		}
		
		
		return array_merge($aPid,$aUnion);
	}

  

  

分类:

技术点:

相关文章:

  • 2022-01-06
  • 2017-11-29
  • 2021-10-18
  • 2021-09-24
  • 2021-12-13
  • 2021-10-18
  • 2021-10-28
  • 2021-12-26
猜你喜欢
  • 2021-08-03
  • 2021-10-18
  • 2021-12-19
  • 2021-08-06
  • 2021-10-18
  • 2021-06-05
  • 2021-09-23
相关资源
相似解决方案