代码如下:
404_3@PHP @H_404_3@/** @H_404_3@* 功能: 根据条件建立分类缓存减少类别使用 @H_404_3@* 创建日期:Thu May 31 15:55:11 CST 2007 @H_404_3@* 最后更新: @H_404_3@* 作者: sanshi @H_404_3@*/ class treeCache @H_404_3@{ @H_404_3@var $tableName = "index_category"; //表名 @H_404_3@var $where = "1"; //where条件 @H_404_3@var $pidStr ="i_c_pid"; //pid 的字段名 @H_404_3@var $tempCode = array(); //生成文件的数组 @H_404_3@var $pid = '0'; //pid的初始值 @H_404_3@var $db ; //数据库句柄 @H_404_3@var $idStr="i_c_id"; //取得的数据id @H_404_3@var $title = "i_c_cn"; //名字字段 @H_404_3@var $createArrayName = "treeCache"; //建立的数组名字 @H_404_3@var $createFileName =""; //建立文件的名字 @H_404_3@var $appendArr = array(); //附加的属性,需要字段名与数据里的名对应 @H_404_3@var $is_utf8 = false; @H_404_3@function treeCache() @H_404_3@{ @H_404_3@} @H_404_3@function set($db) @H_404_3@{ @H_404_3@$this->db = $db; @H_404_3@$this->tempCode[] = "PHP"; @H_404_3@} @H_404_3@//取得所有的一级 @H_404_3@function getRootID() @H_404_3@{ @H_404_3@$sql = "SELECT {$this->idStr} FROM {$this->tableName} WHERE @H_404_3@{$this->pidStr}='{$this->pid}' AND {$this->where} "; @H_404_3@//exit($sql); @H_404_3@$result = $this->db->select($sql); @H_404_3@$temp = array(); @H_404_3@foreach ($result as $r) @H_404_3@{ @H_404_3@$temp[]=$r["{$this->idStr}"]; @H_404_3@} @H_404_3@$this->tempCode[] = "\${$this->createArrayName}['root']='".implode(',',$temp)."';"; @H_404_3@//print_r($temp); @H_404_3@return $temp; @H_404_3@} @H_404_3@//取得子id @H_404_3@function getChildren($pid) @H_404_3@{ @H_404_3@$sql = "SELECT {$this->idStr} FROM {$this->tableName} WHERE @H_404_3@{$this->pidStr}='{$pid}' AND {$this->where} "; @H_404_3@$result = $this->db->select($sql); @H_404_3@$temp = array(); @H_404_3@foreach ($result as $r) @H_404_3@{ @H_404_3@$temp[]=$r["{$this->idStr}"]; @H_404_3@} @H_404_3@return $temp; @H_404_3@} @H_404_3@//取得夫id @H_404_3@function getParent($cid) @H_404_3@{ @H_404_3@$sql = "SELECT {$this->pidStr} FROM {$this->tableName} WHERE @H_404_3@{$this->idStr}='{$cid}' AND {$this->where} "; @H_404_3@$result = $this->db->select($sql); @H_404_3@//print_r($result);exit(); @H_404_3@return $result[0]["{$this->pidStr}"]; @H_404_3@} @H_404_3@//取得上级的id @H_404_3@function getPidStr($cid,$pidStr="") @H_404_3@{ @H_404_3@$pid=$this->getParent($cid); @H_404_3@$temp = array(); @H_404_3@while ($pid!=$this->pid && !emptyempty($pid)) { @H_404_3@$temp[] = $pid; @H_404_3@$pid=$this->getParent($pid); @H_404_3@} @H_404_3@//print_r($temp); @H_404_3@return implode(',$temp); @H_404_3@} @H_404_3@//取得深度 @H_404_3@function getDepth($cid,$depth=0) @H_404_3@{ @H_404_3@$pid=$this->getParent($cid); @H_404_3@$depth++; @H_404_3@if( $pid != $this->pid && !emptyempty($pid)) @H_404_3@$depth = $this->getDepth($pid,$depth); @H_404_3@return $depth; @H_404_3@} @H_404_3@//建立文件 @H_404_3@function make() @H_404_3@{ @H_404_3@if(emptyempty($this->createFileName)) @H_404_3@$this->createFileName = "{$this->createArrayName}.data.PHP"; $rootArr = $this->getRootID(); @H_404_3@$selectF = "{$this->idStr},{$this->title},{$this->pidStr}"; @H_404_3@foreach ($this->appendArr as $app) @H_404_3@{ @H_404_3@if(emptyempty($app)) continue; @H_404_3@$selectF .=",{$app}"; @H_404_3@} @H_404_3@$sql = "SELECT {$selectF} FROM {$this->tableName} WHERE @H_404_3@{$this->where}"; @H_404_3@$result = $this->db->select($sql); @H_404_3@for ($i=0;$i404_3@{ @H_404_3@//id值 @H_404_3@$this->tempCode[] = @H_404_3@"\${$this->createArrayName}['{$result[$i][$this->idStr]}']['id']='{$result[$i]["{$this->idStr}"]}';"; @H_404_3@//标题 @H_404_3@$this->tempCode[] = @H_404_3@"\${$this->createArrayName}['{$result[$i][$this->idStr]}']['title']='{$result[$i]["{$this->title}"]}';"; @H_404_3@//父id @H_404_3@$this->tempCode[] = @H_404_3@"\${$this->createArrayName}['{$result[$i][$this->idStr]}']['pid']='{$result[$i]["{$this->pidStr}"]}';"; @H_404_3@//子id @H_404_3@$this->tempCode[] = @H_404_3@"\${$this->createArrayName}['{$result[$i][$this->idStr]}']['cid']='".implode(',$this->getChildren($result[$i]["$this->idStr"]))."';"; @H_404_3@//目录深度 @H_404_3@$this->tempCode[] = @H_404_3@"\${$this->createArrayName}['{$result[$i][$this->idStr]}']['depth']='".$this->getDepth($result[$i]["$this->idStr"])."';"; @H_404_3@//父id的id串 @H_404_3@$this->tempCode[] = @H_404_3@"\${$this->createArrayName}['{$result[$i][$this->idStr]}']['pstr']='".$this->getPidStr($result[$i]["$this->idStr"])."';"; @H_404_3@//添加的附加属性 @H_404_3@foreach ($this->appendArr as $app) @H_404_3@{ @H_404_3@if(emptyempty($app)) continue; @H_404_3@$this->tempCode[] = @H_404_3@"\${$this->createArrayName}['{$result[$i][$this->idStr]}']['{$app}']='{$result[$i]["{$app}"]}';"; @H_404_3@} @H_404_3@} @H_404_3@$this->tempCode[] = "return \${$this->createArrayName};"; @H_404_3@$this->tempCode[] = "?>"; @H_404_3@//$content = implode("\n",$this->tempCode); @H_404_3@//print_r($this->tempCode); @H_404_3@$content = implode("\n",$this->tempCode); @H_404_3@//建立文件 @H_404_3@$fio=Factory::getBaseClass('FileIO'); @H_404_3@if($this->is_utf8) $content = "\xEF\xBB\xBF".$content; @H_404_3@$fio->writeFile($this->createFileName,$content); @H_404_3@return $content ; @H_404_3@} @H_404_3@} @H_404_3@//加载的文件就是为了,数据库连接 @H_404_3@//需要数据库有 select 方法 @H_404_3@/* @H_404_3@include_once(dirname(dirname(__FILE__))."/config/config.inc.PHP"); @H_404_3@include_once(CLASSES_PATH."factryObject.class.PHP"); $db = factryObject::getDB('indexPush'); @H_404_3@$c = new treeCache($db); @H_404_3@$c->make(); @H_404_3@//exit(); //做分析 @H_404_3@include_once("treeCache.data.PHP"); @H_404_3@$treeCache=isset($treeCache) ? $treeCache : array(); @H_404_3@$rootStr = isset($treeCache['root']) ? $treeCache['root'] : ""; @H_404_3@echo parseTree($treeCache,$rootStr); @H_404_3@function parseTree($treeCache,$rootStr) @H_404_3@{ @H_404_3@$tempStr = ""; @H_404_3@$temp = explode(',$rootStr); @H_404_3@foreach ($temp AS $cid) @H_404_3@{ @H_404_3@$info = $treeCache[$cid]; @H_404_3@$cidStr = $info['cid']; @H_404_3@$tempStr .= str_repeat('-',($info['depth']-1)*3); @H_404_3@$tempStr.=$info['title']; @H_404_3@if(empty($info['pid'])) @H_404_3@{ @H_404_3@//附加操作 @H_404_3@} @H_404_3@$tempStr .= "
"; @H_404_3@if(!empty($info['cid'])) @H_404_3@$tempStr .=parseTree($treeCache,$info['cid']); @H_404_3@} @H_404_3@return $tempStr; @H_404_3@} @H_404_3@*/ @H_404_3@?> @H_404_3@PHP @H_404_3@/** @H_404_3@* 功能: 根据条件建立分类缓存减少类别使用 @H_404_3@* 创建日期:Thu May 31 15:55:11 CST 2007 @H_404_3@* 最后更新: @H_404_3@* 作者: sanshi @H_404_3@*/ class treeCache @H_404_3@{ @H_404_3@var $tableName = "index_category"; //表名 @H_404_3@var $where = "1"; //where条件 @H_404_3@var $pidStr ="i_c_pid"; //pid 的字段名 @H_404_3@var $tempCode = array(); //生成文件的数组 @H_404_3@var $pid = '0'; //pid的初始值 @H_404_3@var $db ; //数据库句柄 @H_404_3@var $idStr="i_c_id"; //取得的数据id @H_404_3@var $title = "i_c_cn"; //名字字段 @H_404_3@var $createArrayName = "treeCache"; //建立的数组名字 @H_404_3@var $createFileName =""; //建立文件的名字 @H_404_3@var $appendArr = array(); //附加的属性,需要字段名与数据里的名对应 @H_404_3@var $is_utf8 = false; @H_404_3@function treeCache() @H_404_3@{ @H_404_3@} @H_404_3@function set($db) @H_404_3@{ @H_404_3@$this->db = $db; @H_404_3@$this->tempCode[] = "PHP"; @H_404_3@} @H_404_3@//取得所有的一级 @H_404_3@function getRootID() @H_404_3@{ @H_404_3@$sql = "SELECT {$this->idStr} FROM {$this->tableName} WHERE @H_404_3@{$this->pidStr}='{$this->pid}' AND {$this->where} "; @H_404_3@//exit($sql); @H_404_3@$result = $this->db->select($sql); @H_404_3@$temp = array(); @H_404_3@foreach ($result as $r) @H_404_3@{ @H_404_3@$temp[]=$r["{$this->idStr}"]; @H_404_3@} @H_404_3@$this->tempCode[] = "\${$this->createArrayName}['root']='".implode(',$pidStr="") @H_404_3@{ @H_404_3@$pid=$this->getParent($cid); @H_404_3@$temp = array(); @H_404_3@while ($pid!=$this->pid && !empty($pid)) { @H_404_3@$temp[] = $pid; @H_404_3@$pid=$this->getParent($pid); @H_404_3@} @H_404_3@//print_r($temp); @H_404_3@return implode(',$depth=0) @H_404_3@{ @H_404_3@$pid=$this->getParent($cid); @H_404_3@$depth++; @H_404_3@if( $pid != $this->pid && !empty($pid)) @H_404_3@$depth = $this->getDepth($pid,$depth); @H_404_3@return $depth; @H_404_3@} @H_404_3@//建立文件 @H_404_3@function make() @H_404_3@{ @H_404_3@if(empty($this->createFileName)) @H_404_3@$this->createFileName = "{$this->createArrayName}.data.PHP"; $rootArr = $this->getRootID(); @H_404_3@$selectF = "{$this->idStr},{$this->pidStr}"; @H_404_3@foreach ($this->appendArr as $app) @H_404_3@{ @H_404_3@if(empty($app)) continue; @H_404_3@$selectF .=",$this->getChildren($result[$i]["$this->idStr"]))."';"; @H_404_3@//目录深度 @H_404_3@$this->tempCode[] = @H_404_3@"\${$this->createArrayName}['{$result[$i][$this->idStr]}']['depth']='".$this->getDepth($result[$i]["$this->idStr"])."';"; @H_404_3@//父id的id串 @H_404_3@$this->tempCode[] = @H_404_3@"\${$this->createArrayName}['{$result[$i][$this->idStr]}']['pstr']='".$this->getPidStr($result[$i]["$this->idStr"])."';"; @H_404_3@//添加的附加属性 @H_404_3@foreach ($this->appendArr as $app) @H_404_3@{ @H_404_3@if(empty($app)) continue; @H_404_3@$this->tempCode[] = @H_404_3@"\${$this->createArrayName}['{$result[$i][$this->idStr]}']['{$app}']='{$result[$i]["{$app}"]}';"; @H_404_3@} @H_404_3@} @H_404_3@$this->tempCode[] = "return \${$this->createArrayName};"; @H_404_3@$this->tempCode[] = "?>"; @H_404_3@//$content = implode("\n",$info['cid']); @H_404_3@} @H_404_3@return $tempStr; @H_404_3@} @H_404_3@*/ @H_404_3@?> 这个类是改善过的,当初做这个的适合,只能是数字的作为数组下标,这次支持字母了 另外就是关于解析 view plaincopy to clipboardprint? @H_404_3@PHP @H_404_3@class parseTree @H_404_3@{ @H_404_3@var $ads_type_file = ""; @H_404_3@var $isX = false; @H_404_3@var $rowSize=2; @H_404_3@function parseTree() @H_404_3@{ @H_404_3@$this->ads_type_file = CACHE_PATH."ads_type_arr.data.PHP"; @H_404_3@$this->ads_city_file = CACHE_PATH."ads_city_arr.data.PHP"; @H_404_3@} @H_404_3@function make_ads_type() @H_404_3@{ @H_404_3@$db = Factory::getDB("ads_type"); @H_404_3@$tree =Factory::getItemClass('treeCache'); @H_404_3@$tree->set($db); @H_404_3@$tree->tableName=$db->tableName; @H_404_3@$tree->pidStr ="ads_type_pid"; @H_404_3@$tree->idStr = "ads_type_id"; @H_404_3@$tree->title = "ads_type_name"; @H_404_3@$tree->createArrayName ="ads_type_arr"; @H_404_3@$tree->where = " ads_type_state=1 ORDER BY ads_type_id DESC "; @H_404_3@$tree->appendArr = array("ads_type_info"); @H_404_3@$tree->createFileName = $this->ads_type_file; @H_404_3@$tree->is_utf8 = true; @H_404_3@return $tree->make(); @H_404_3@} @H_404_3@function get_ads_type_str() @H_404_3@{ @H_404_3@$temp_arr = $this->get_ads_type_arr(); @H_404_3@$treeArr = emptyempty($temp_arr) ? array() : $temp_arr; @H_404_3@$rootStr = isset($temp_arr['root']) ? $temp_arr['root'] : ""; @H_404_3@$show_content = $this->__parseTree($treeArr,$rootStr,'pares_type_link'); @H_404_3@return $show_content; @H_404_3@} @H_404_3@function get_ads_type_arr() @H_404_3@{ @H_404_3@return is_file($this->ads_type_file) ? require($this->ads_type_file) : array(); @H_404_3@} @H_404_3@function pares_type_link($info) @H_404_3@{ @H_404_3@$class_name = "ads_type"; @H_404_3@$tempStr = "[子-添加]"; @H_404_3@$tempStr .="[编辑]"; @H_404_3@$tempStr .="[删除]"; @H_404_3@return $tempStr; @H_404_3@} @H_404_3@//根据提供的类别号取得类别数组 @H_404_3@function get_type_arr($type_no=0) @H_404_3@{ @H_404_3@$temp_arr = $this->get_ads_type_arr(); @H_404_3@$rootStr = $type_no==0 ? (isset($temp_arr['root'])?$temp_arr['root'] : "") : (isset($temp_arr[$type_no]['cid'])?$temp_arr[$type_no]['cid']:""); @H_404_3@$temp = explode(',$rootStr); @H_404_3@$return_temp = array(); @H_404_3@foreach($temp as $cid) @H_404_3@{ @H_404_3@if(isset($temp_arr[$cid])) $return_temp[$temp_arr[$cid]['id']]=$temp_arr[$cid]['title']; @H_404_3@} @H_404_3@return $return_temp; @H_404_3@} function make_ads_city() @H_404_3@{ @H_404_3@$db = Factory::getDB("ads_city"); @H_404_3@$tree =Factory::getItemClass('treeCache'); @H_404_3@$tree->set($db); @H_404_3@$tree->tableName=$db->tableName; @H_404_3@$tree->pidStr ="ads_city_pid"; @H_404_3@$tree->idStr = "ads_city_no"; @H_404_3@$tree->title = "ads_city_name"; @H_404_3@$tree->createArrayName ="ads_city_arr"; @H_404_3@$tree->where = " ads_city_state=1 ORDER BY ads_city_no DESC "; @H_404_3@$tree->appendArr = array("ads_city_info"); @H_404_3@$tree->createFileName = $this->ads_city_file; @H_404_3@$tree->is_utf8 = true; @H_404_3@return $tree->make(); @H_404_3@} @H_404_3@function get_ads_city_arr() @H_404_3@{ @H_404_3@return is_file($this->ads_city_file) ? require($this->ads_city_file) : array(); @H_404_3@} @H_404_3@function get_ads_city_str() @H_404_3@{ @H_404_3@$temp_arr = $this->get_ads_city_arr(); @H_404_3@$treeArr = emptyempty($temp_arr) ? array() : $temp_arr; @H_404_3@$rootStr = isset($temp_arr['root']) ? $temp_arr['root'] : ""; @H_404_3@$show_content = $this->__parseTree($treeArr,'pares_city_link'); @H_404_3@return $show_content; @H_404_3@} @H_404_3@function pares_city_link($info) @H_404_3@{ @H_404_3@$class_name = "ads_city"; @H_404_3@$tempStr = "[子-添加]"; @H_404_3@$tempStr .="[编辑]"; @H_404_3@$tempStr .="[删除]"; @H_404_3@return $tempStr; @H_404_3@} @H_404_3@//根据提供的城市号取得城市数组 @H_404_3@function get_city_arr($city_no=0) @H_404_3@{ @H_404_3@$temp_arr = $this->get_ads_city_arr(); @H_404_3@$rootStr = $city_no==0 ? (isset($temp_arr['root'])?$temp_arr['root'] : "") : (isset($temp_arr[$city_no])?$temp_arr[$city_no]:""); @H_404_3@$temp = explode(',$rootStr); @H_404_3@$return_temp = array(); @H_404_3@foreach($temp as $cid) @H_404_3@{ @H_404_3@if(isset($temp_arr[$cid])) $return_temp[$temp_arr[$cid]['id']]=$temp_arr[$cid]['title']; @H_404_3@} @H_404_3@return $return_temp; @H_404_3@} function __parseTree($treeCache,$fuc_str) @H_404_3@{ @H_404_3@$tempStr = ""; @H_404_3@$temp = explode(',$rootStr); @H_404_3@if(emptyempty($temp)) return ""; @H_404_3@$this->layer=0; @H_404_3@foreach ($temp AS $cid) @H_404_3@{ @H_404_3@if(isset($treeCache[$cid])) @H_404_3@{ @H_404_3@$info = $treeCache[$cid]; @H_404_3@$cidStr = $info['cid']; @H_404_3@//如果下面有子id @H_404_3@if($info["cid"]!="") @H_404_3@{ @H_404_3@$tempStr .="
"; @H_404_3@else @H_404_3@$tempStr .= " "; @H_404_3@}else{ @H_404_3@$tempStr .= "
"; @H_404_3@} @H_404_3@} @H_404_3@} @H_404_3@} @H_404_3@return $tempStr; @H_404_3@} @H_404_3@} @H_404_3@?> @H_404_3@
"; @H_404_3@if(!empty($info['cid'])) @H_404_3@$tempStr .=parseTree($treeCache,$info['cid']); @H_404_3@} @H_404_3@return $tempStr; @H_404_3@} @H_404_3@*/ @H_404_3@?> @H_404_3@PHP @H_404_3@/** @H_404_3@* 功能: 根据条件建立分类缓存减少类别使用 @H_404_3@* 创建日期:Thu May 31 15:55:11 CST 2007 @H_404_3@* 最后更新: @H_404_3@* 作者: sanshi
"; @H_404_3@$tempStr .= str_repeat(' ',($info['depth']-1)*3); @H_404_3@if($info["cid"]!="") @H_404_3@{ @H_404_3@//$tempStr .="
"; @H_404_3@} @H_404_3@//$tempStr .=">"; @H_404_3@$tempStr.=$info['title']; @H_404_3@$tempStr .=$this->{$fuc_str}($info); @H_404_3@$tempStr .="
"; @H_404_3@$tempStr .=""; @H_404_3@}else{ @H_404_3@$this->layer++; @H_404_3@$tempStr .= str_repeat(' ',($info['depth']-1)*3); @H_404_3@$tempStr.=$info['title']; @H_404_3@$tempStr .=$this->{$fuc_str}($info); @H_404_3@if($this->isX==true) @H_404_3@{ @H_404_3@if($this->layer % $this->rowSize ==0) @H_404_3@$tempStr .= "
"; @H_404_3@else @H_404_3@$tempStr .= " "; @H_404_3@}else{ @H_404_3@$tempStr .= "
"; @H_404_3@} @H_404_3@} @H_404_3@} @H_404_3@} @H_404_3@return $tempStr; @H_404_3@} @H_404_3@} @H_404_3@?> @H_404_3@