php 无限级缓存的类的扩展
程序员文章站
2022-05-20 17:05:53
复制代码 代码如下:
<?php
/**
* 功能: 根据条件建立分类缓存减少类别使用
* 创建日期:thu may 31 15:55:11 cst 2007
* 最后更新:
* 作者: sanshi <sanshi0815@tom.com>
*/
class treecache
{
var $tablename = "index_category"; //表名
var $where = "1"; //where条件
var $pidstr ="i_c_pid"; //pid 的字段名
var $tempcode = array(); //生成文件的数组
var $pid = '0'; //pid的初始值
var $db ; //数据库句柄
var $idstr="i_c_id"; //取得的数据id
var $title = "i_c_cn"; //名字字段
var $createarrayname = "treecache"; //建立的数组名字
var $createfilename =""; //建立文件的名字
var $appendarr = array(); //附加的属性,需要字段名与数据里的名对应
var $is_utf8 = false;
function treecache()
{
}
function set($db)
{
$this->db = $db;
$this->tempcode[] = "<?php";
}
//取得所有的一级
function getrootid()
{
$sql = "select {$this->idstr} from {$this->tablename} where
{$this->pidstr}='{$this->pid}' and {$this->where} ";
//exit($sql);
$result = $this->db->select($sql);
$temp = array();
foreach ($result as $r)
{
$temp[]=$r["{$this->idstr}"];
}
$this->tempcode[] = "\${$this->createarrayname}['root']='".implode(',',$temp)."';";
//print_r($temp);
return $temp;
}
//取得子id
function getchildren($pid)
{
$sql = "select {$this->idstr} from {$this->tablename} where
{$this->pidstr}='{$pid}' and {$this->where} ";
$result = $this->db->select($sql);
$temp = array();
foreach ($result as $r)
{
$temp[]=$r["{$this->idstr}"];
}
return $temp;
}
//取得夫id
function getparent($cid)
{
$sql = "select {$this->pidstr} from {$this->tablename} where
{$this->idstr}='{$cid}' and {$this->where} ";
$result = $this->db->select($sql);
//print_r($result);exit();
return $result[0]["{$this->pidstr}"];
}
//取得上级的id
function getpidstr($cid,$pidstr="")
{
$pid=$this->getparent($cid);
$temp = array();
while ($pid!=$this->pid && !emptyempty($pid)) {
$temp[] = $pid;
$pid=$this->getparent($pid);
}
//print_r($temp);
return implode(',',$temp);
}
//取得深度
function getdepth($cid,$depth=0)
{
$pid=$this->getparent($cid);
$depth++;
if( $pid != $this->pid && !emptyempty($pid))
$depth = $this->getdepth($pid,$depth);
return $depth;
}
//建立文件
function make()
{
if(emptyempty($this->createfilename))
$this->createfilename = "{$this->createarrayname}.data.php";
$rootarr = $this->getrootid();
$selectf = "{$this->idstr},{$this->title},{$this->pidstr}";
foreach ($this->appendarr as $app)
{
if(emptyempty($app)) continue;
$selectf .=",{$app}";
}
$sql = "select {$selectf} from {$this->tablename} where
{$this->where}";
$result = $this->db->select($sql);
for ($i=0;$i<count($result);$i++)
{
//id值
$this->tempcode[] =
"\${$this->createarrayname}['{$result[$i][$this->idstr]}']['id']='{$result[$i]["{$this->idstr}"]}';";
//标题
$this->tempcode[] =
"\${$this->createarrayname}['{$result[$i][$this->idstr]}']['title']='{$result[$i]["{$this->title}"]}';";
//父id
$this->tempcode[] =
"\${$this->createarrayname}['{$result[$i][$this->idstr]}']['pid']='{$result[$i]["{$this->pidstr}"]}';";
//子id
$this->tempcode[] =
"\${$this->createarrayname}['{$result[$i][$this->idstr]}']['cid']='".implode(',',$this->getchildren($result[$i]["$this->idstr"]))."';";
//目录深度
$this->tempcode[] =
"\${$this->createarrayname}['{$result[$i][$this->idstr]}']['depth']='".$this->getdepth($result[$i]["$this->idstr"])."';";
//父id的id串
$this->tempcode[] =
"\${$this->createarrayname}['{$result[$i][$this->idstr]}']['pstr']='".$this->getpidstr($result[$i]["$this->idstr"])."';";
//添加的附加属性
foreach ($this->appendarr as $app)
{
if(emptyempty($app)) continue;
$this->tempcode[] =
"\${$this->createarrayname}['{$result[$i][$this->idstr]}']['{$app}']='{$result[$i]["{$app}"]}';";
}
}
$this->tempcode[] = "return \${$this->createarrayname};";
$this->tempcode[] = "?>";
//$content = implode("\n",$this->tempcode);
//print_r($this->tempcode);
$content = implode("\n",$this->tempcode);
//建立文件
$fio=factory::getbaseclass('fileio');
if($this->is_utf8) $content = "\xef\xbb\xbf".$content;
$fio->writefile($this->createfilename,$content);
return $content ;
}
}
//加载的文件就是为了,数据库连接
//需要数据库有 select 方法
/*
include_once(dirname(dirname(__file__))."/config/config.inc.php");
include_once(classes_path."factryobject.class.php");
$db = factryobject::getdb('indexpush');
$c = new treecache($db);
$c->make();
//exit();
//做分析
include_once("treecache.data.php");
$treecache=isset($treecache) ? $treecache : array();
$rootstr = isset($treecache['root']) ? $treecache['root'] : "";
echo parsetree($treecache,$rootstr);
function parsetree($treecache,$rootstr)
{
$tempstr = "";
$temp = explode(',',$rootstr);
foreach ($temp as $cid)
{
$info = $treecache[$cid];
$cidstr = $info['cid'];
$tempstr .= str_repeat('-',($info['depth']-1)*3);
$tempstr.=$info['title'];
if(empty($info['pid']))
{
//附加操作
}
$tempstr .= "<br/>";
if(!empty($info['cid']))
$tempstr .=parsetree($treecache,$info['cid']);
}
return $tempstr;
}
*/
?>
<?php
/**
* 功能: 根据条件建立分类缓存减少类别使用
* 创建日期:thu may 31 15:55:11 cst 2007
* 最后更新:
* 作者: sanshi <sanshi0815@tom.com>
*/
class treecache
{
var $tablename = "index_category"; //表名
var $where = "1"; //where条件
var $pidstr ="i_c_pid"; //pid 的字段名
var $tempcode = array(); //生成文件的数组
var $pid = '0'; //pid的初始值
var $db ; //数据库句柄
var $idstr="i_c_id"; //取得的数据id
var $title = "i_c_cn"; //名字字段
var $createarrayname = "treecache"; //建立的数组名字
var $createfilename =""; //建立文件的名字
var $appendarr = array(); //附加的属性,需要字段名与数据里的名对应
var $is_utf8 = false;
function treecache()
{
}
function set($db)
{
$this->db = $db;
$this->tempcode[] = "<?php";
}
//取得所有的一级
function getrootid()
{
$sql = "select {$this->idstr} from {$this->tablename} where
{$this->pidstr}='{$this->pid}' and {$this->where} ";
//exit($sql);
$result = $this->db->select($sql);
$temp = array();
foreach ($result as $r)
{
$temp[]=$r["{$this->idstr}"];
}
$this->tempcode[] = "\${$this->createarrayname}['root']='".implode(',',$temp)."';";
//print_r($temp);
return $temp;
}
//取得子id
function getchildren($pid)
{
$sql = "select {$this->idstr} from {$this->tablename} where
{$this->pidstr}='{$pid}' and {$this->where} ";
$result = $this->db->select($sql);
$temp = array();
foreach ($result as $r)
{
$temp[]=$r["{$this->idstr}"];
}
return $temp;
}
//取得夫id
function getparent($cid)
{
$sql = "select {$this->pidstr} from {$this->tablename} where
{$this->idstr}='{$cid}' and {$this->where} ";
$result = $this->db->select($sql);
//print_r($result);exit();
return $result[0]["{$this->pidstr}"];
}
//取得上级的id
function getpidstr($cid,$pidstr="")
{
$pid=$this->getparent($cid);
$temp = array();
while ($pid!=$this->pid && !empty($pid)) {
$temp[] = $pid;
$pid=$this->getparent($pid);
}
//print_r($temp);
return implode(',',$temp);
}
//取得深度
function getdepth($cid,$depth=0)
{
$pid=$this->getparent($cid);
$depth++;
if( $pid != $this->pid && !empty($pid))
$depth = $this->getdepth($pid,$depth);
return $depth;
}
//建立文件
function make()
{
if(empty($this->createfilename))
$this->createfilename = "{$this->createarrayname}.data.php";
$rootarr = $this->getrootid();
$selectf = "{$this->idstr},{$this->title},{$this->pidstr}";
foreach ($this->appendarr as $app)
{
if(empty($app)) continue;
$selectf .=",{$app}";
}
$sql = "select {$selectf} from {$this->tablename} where
{$this->where}";
$result = $this->db->select($sql);
for ($i=0;$i<count($result);$i++)
{
//id值
$this->tempcode[] =
"\${$this->createarrayname}['{$result[$i][$this->idstr]}']['id']='{$result[$i]["{$this->idstr}"]}';";
//标题
$this->tempcode[] =
"\${$this->createarrayname}['{$result[$i][$this->idstr]}']['title']='{$result[$i]["{$this->title}"]}';";
//父id
$this->tempcode[] =
"\${$this->createarrayname}['{$result[$i][$this->idstr]}']['pid']='{$result[$i]["{$this->pidstr}"]}';";
//子id
$this->tempcode[] =
"\${$this->createarrayname}['{$result[$i][$this->idstr]}']['cid']='".implode(',',$this->getchildren($result[$i]["$this->idstr"]))."';";
//目录深度
$this->tempcode[] =
"\${$this->createarrayname}['{$result[$i][$this->idstr]}']['depth']='".$this->getdepth($result[$i]["$this->idstr"])."';";
//父id的id串
$this->tempcode[] =
"\${$this->createarrayname}['{$result[$i][$this->idstr]}']['pstr']='".$this->getpidstr($result[$i]["$this->idstr"])."';";
//添加的附加属性
foreach ($this->appendarr as $app)
{
if(empty($app)) continue;
$this->tempcode[] =
"\${$this->createarrayname}['{$result[$i][$this->idstr]}']['{$app}']='{$result[$i]["{$app}"]}';";
}
}
$this->tempcode[] = "return \${$this->createarrayname};";
$this->tempcode[] = "?>";
//$content = implode("\n",$this->tempcode);
//print_r($this->tempcode);
$content = implode("\n",$this->tempcode);
//建立文件
$fio=factory::getbaseclass('fileio');
if($this->is_utf8) $content = "\xef\xbb\xbf".$content;
$fio->writefile($this->createfilename,$content);
return $content ;
}
}
//加载的文件就是为了,数据库连接
//需要数据库有 select 方法
/*
include_once(dirname(dirname(__file__))."/config/config.inc.php");
include_once(classes_path."factryobject.class.php");
$db = factryobject::getdb('indexpush');
$c = new treecache($db);
$c->make();
//exit();
//做分析
include_once("treecache.data.php");
$treecache=isset($treecache) ? $treecache : array();
$rootstr = isset($treecache['root']) ? $treecache['root'] : "";
echo parsetree($treecache,$rootstr);
function parsetree($treecache,$rootstr)
{
$tempstr = "";
$temp = explode(',',$rootstr);
foreach ($temp as $cid)
{
$info = $treecache[$cid];
$cidstr = $info['cid'];
$tempstr .= str_repeat('-',($info['depth']-1)*3);
$tempstr.=$info['title'];
if(empty($info['pid']))
{
//附加操作
}
$tempstr .= "<br/>";
if(!empty($info['cid']))
$tempstr .=parsetree($treecache,$info['cid']);
}
return $tempstr;
}
*/
?>
这个类是改善过的,当初做这个的适合,只能是数字的作为数组下标,这次支持字母了
另外就是关于解析
view plaincopy to clipboardprint?
<?php
class parsetree
{
var $ads_type_file = "";
var $isx = false;
var $rowsize=2;
function parsetree()
{
$this->ads_type_file = cache_path."ads_type_arr.data.php";
$this->ads_city_file = cache_path."ads_city_arr.data.php";
}
function make_ads_type()
{
$db = factory::getdb("ads_type");
$tree =factory::getitemclass('treecache');
$tree->set($db);
$tree->tablename=$db->tablename;
$tree->pidstr ="ads_type_pid";
$tree->idstr = "ads_type_id";
$tree->title = "ads_type_name";
$tree->createarrayname ="ads_type_arr";
$tree->where = " ads_type_state=1 order by ads_type_id desc ";
$tree->appendarr = array("ads_type_info");
$tree->createfilename = $this->ads_type_file;
$tree->is_utf8 = true;
return $tree->make();
}
function get_ads_type_str()
{
$temp_arr = $this->get_ads_type_arr();
$treearr = emptyempty($temp_arr) ? array() : $temp_arr;
$rootstr = isset($temp_arr['root']) ? $temp_arr['root'] : "";
$show_content = $this->__parsetree($treearr,$rootstr,'pares_type_link');
return $show_content;
}
function get_ads_type_arr()
{
return is_file($this->ads_type_file) ? require($this->ads_type_file) : array();
}
function pares_type_link($info)
{
$class_name = "ads_type";
$tempstr = "[<a href="?c={$class_name}&m=add&ads_type_pid={$info[" href="?c={$class_name}&m=add&ads_type_pid={$info["id']}'>子-添加</a>]";
$tempstr .="[<a href="?c={$class_name}&m=edit&ads_type_id={$info[" href="?c={$class_name}&m=edit&ads_type_id={$info["id']}'>编辑</a>]";
$tempstr .="[<a href="?c={$class_name}&m=del&ads_type_id={$info[" href="?c={$class_name}&m=del&ads_type_id={$info["id']}'>删除</a>]";
return $tempstr;
}
//根据提供的类别号取得类别数组
function get_type_arr($type_no=0)
{
$temp_arr = $this->get_ads_type_arr();
$rootstr = $type_no==0 ? (isset($temp_arr['root'])?$temp_arr['root'] : "") : (isset($temp_arr[$type_no]['cid'])?$temp_arr[$type_no]['cid']:"");
$temp = explode(',',$rootstr);
$return_temp = array();
foreach($temp as $cid)
{
if(isset($temp_arr[$cid])) $return_temp[$temp_arr[$cid]['id']]=$temp_arr[$cid]['title'];
}
return $return_temp;
}
function make_ads_city()
{
$db = factory::getdb("ads_city");
$tree =factory::getitemclass('treecache');
$tree->set($db);
$tree->tablename=$db->tablename;
$tree->pidstr ="ads_city_pid";
$tree->idstr = "ads_city_no";
$tree->title = "ads_city_name";
$tree->createarrayname ="ads_city_arr";
$tree->where = " ads_city_state=1 order by ads_city_no desc ";
$tree->appendarr = array("ads_city_info");
$tree->createfilename = $this->ads_city_file;
$tree->is_utf8 = true;
return $tree->make();
}
function get_ads_city_arr()
{
return is_file($this->ads_city_file) ? require($this->ads_city_file) : array();
}
function get_ads_city_str()
{
$temp_arr = $this->get_ads_city_arr();
$treearr = emptyempty($temp_arr) ? array() : $temp_arr;
$rootstr = isset($temp_arr['root']) ? $temp_arr['root'] : "";
$show_content = $this->__parsetree($treearr,$rootstr,'pares_city_link');
return $show_content;
}
function pares_city_link($info)
{
$class_name = "ads_city";
$tempstr = "[<a href="?c={$class_name}&m=add&ads_city_pid={$info[" href="?c={$class_name}&m=add&ads_city_pid={$info["id']}'>子-添加</a>]";
$tempstr .="[<a href="?c={$class_name}&m=edit&ads_city_no={$info[" href="?c={$class_name}&m=edit&ads_city_no={$info["id']}'>编辑</a>]";
$tempstr .="[<a href="?c={$class_name}&m=del&ads_city_no={$info[" href="?c={$class_name}&m=del&ads_city_no={$info["id']}'>删除</a>]";
return $tempstr;
}
//根据提供的城市号取得城市数组
function get_city_arr($city_no=0)
{
$temp_arr = $this->get_ads_city_arr();
$rootstr = $city_no==0 ? (isset($temp_arr['root'])?$temp_arr['root'] : "") : (isset($temp_arr[$city_no])?$temp_arr[$city_no]:"");
$temp = explode(',',$rootstr);
$return_temp = array();
foreach($temp as $cid)
{
if(isset($temp_arr[$cid])) $return_temp[$temp_arr[$cid]['id']]=$temp_arr[$cid]['title'];
}
return $return_temp;
}
function __parsetree($treecache,$rootstr,$fuc_str)
{
$tempstr = "";
$temp = explode(',',$rootstr);
if(emptyempty($temp)) return "";
$this->layer=0;
foreach ($temp as $cid)
{
if(isset($treecache[$cid]))
{
$info = $treecache[$cid];
$cidstr = $info['cid'];
//如果下面有子id
if($info["cid"]!="")
{
$tempstr .="<div >";
$tempstr .= str_repeat(' ',($info['depth']-1)*3);
if($info["cid"]!="")
{
//$tempstr .="<img src="/images/arrow.jpg" src="images/arrow.jpg" style="cursor:hand;" style="cursor:hand;" onclick='javascript:showmenu(\"l_{$info['id']}\");' id='pic_l_{$info['id']}'>";
}
//$tempstr .=">";
$tempstr.=$info['title'];
$tempstr .=$this->{$fuc_str}($info);
$tempstr .="</div>";
$tempstr .="<div id='l_{$info['id']}' >";
$tempstr .= $this->__parsetree($treecache,$info['cid'],$fuc_str);
$tempstr .="</div>";
}else{
$this->layer++;
$tempstr .= str_repeat(' ',($info['depth']-1)*3);
$tempstr.=$info['title'];
$tempstr .=$this->{$fuc_str}($info);
if($this->isx==true)
{
if($this->layer % $this->rowsize ==0)
$tempstr .= "<br/>";
else
$tempstr .= " ";
}else{
$tempstr .= "<br/>";
}
}
}
}
return $tempstr;
}
}
?>
复制代码 代码如下:
<?php
/**
* 功能: 根据条件建立分类缓存减少类别使用
* 创建日期:thu may 31 15:55:11 cst 2007
* 最后更新:
* 作者: sanshi <sanshi0815@tom.com>
*/
class treecache
{
var $tablename = "index_category"; //表名
var $where = "1"; //where条件
var $pidstr ="i_c_pid"; //pid 的字段名
var $tempcode = array(); //生成文件的数组
var $pid = '0'; //pid的初始值
var $db ; //数据库句柄
var $idstr="i_c_id"; //取得的数据id
var $title = "i_c_cn"; //名字字段
var $createarrayname = "treecache"; //建立的数组名字
var $createfilename =""; //建立文件的名字
var $appendarr = array(); //附加的属性,需要字段名与数据里的名对应
var $is_utf8 = false;
function treecache()
{
}
function set($db)
{
$this->db = $db;
$this->tempcode[] = "<?php";
}
//取得所有的一级
function getrootid()
{
$sql = "select {$this->idstr} from {$this->tablename} where
{$this->pidstr}='{$this->pid}' and {$this->where} ";
//exit($sql);
$result = $this->db->select($sql);
$temp = array();
foreach ($result as $r)
{
$temp[]=$r["{$this->idstr}"];
}
$this->tempcode[] = "\${$this->createarrayname}['root']='".implode(',',$temp)."';";
//print_r($temp);
return $temp;
}
//取得子id
function getchildren($pid)
{
$sql = "select {$this->idstr} from {$this->tablename} where
{$this->pidstr}='{$pid}' and {$this->where} ";
$result = $this->db->select($sql);
$temp = array();
foreach ($result as $r)
{
$temp[]=$r["{$this->idstr}"];
}
return $temp;
}
//取得夫id
function getparent($cid)
{
$sql = "select {$this->pidstr} from {$this->tablename} where
{$this->idstr}='{$cid}' and {$this->where} ";
$result = $this->db->select($sql);
//print_r($result);exit();
return $result[0]["{$this->pidstr}"];
}
//取得上级的id
function getpidstr($cid,$pidstr="")
{
$pid=$this->getparent($cid);
$temp = array();
while ($pid!=$this->pid && !emptyempty($pid)) {
$temp[] = $pid;
$pid=$this->getparent($pid);
}
//print_r($temp);
return implode(',',$temp);
}
//取得深度
function getdepth($cid,$depth=0)
{
$pid=$this->getparent($cid);
$depth++;
if( $pid != $this->pid && !emptyempty($pid))
$depth = $this->getdepth($pid,$depth);
return $depth;
}
//建立文件
function make()
{
if(emptyempty($this->createfilename))
$this->createfilename = "{$this->createarrayname}.data.php";
$rootarr = $this->getrootid();
$selectf = "{$this->idstr},{$this->title},{$this->pidstr}";
foreach ($this->appendarr as $app)
{
if(emptyempty($app)) continue;
$selectf .=",{$app}";
}
$sql = "select {$selectf} from {$this->tablename} where
{$this->where}";
$result = $this->db->select($sql);
for ($i=0;$i<count($result);$i++)
{
//id值
$this->tempcode[] =
"\${$this->createarrayname}['{$result[$i][$this->idstr]}']['id']='{$result[$i]["{$this->idstr}"]}';";
//标题
$this->tempcode[] =
"\${$this->createarrayname}['{$result[$i][$this->idstr]}']['title']='{$result[$i]["{$this->title}"]}';";
//父id
$this->tempcode[] =
"\${$this->createarrayname}['{$result[$i][$this->idstr]}']['pid']='{$result[$i]["{$this->pidstr}"]}';";
//子id
$this->tempcode[] =
"\${$this->createarrayname}['{$result[$i][$this->idstr]}']['cid']='".implode(',',$this->getchildren($result[$i]["$this->idstr"]))."';";
//目录深度
$this->tempcode[] =
"\${$this->createarrayname}['{$result[$i][$this->idstr]}']['depth']='".$this->getdepth($result[$i]["$this->idstr"])."';";
//父id的id串
$this->tempcode[] =
"\${$this->createarrayname}['{$result[$i][$this->idstr]}']['pstr']='".$this->getpidstr($result[$i]["$this->idstr"])."';";
//添加的附加属性
foreach ($this->appendarr as $app)
{
if(emptyempty($app)) continue;
$this->tempcode[] =
"\${$this->createarrayname}['{$result[$i][$this->idstr]}']['{$app}']='{$result[$i]["{$app}"]}';";
}
}
$this->tempcode[] = "return \${$this->createarrayname};";
$this->tempcode[] = "?>";
//$content = implode("\n",$this->tempcode);
//print_r($this->tempcode);
$content = implode("\n",$this->tempcode);
//建立文件
$fio=factory::getbaseclass('fileio');
if($this->is_utf8) $content = "\xef\xbb\xbf".$content;
$fio->writefile($this->createfilename,$content);
return $content ;
}
}
//加载的文件就是为了,数据库连接
//需要数据库有 select 方法
/*
include_once(dirname(dirname(__file__))."/config/config.inc.php");
include_once(classes_path."factryobject.class.php");
$db = factryobject::getdb('indexpush');
$c = new treecache($db);
$c->make();
//exit();
//做分析
include_once("treecache.data.php");
$treecache=isset($treecache) ? $treecache : array();
$rootstr = isset($treecache['root']) ? $treecache['root'] : "";
echo parsetree($treecache,$rootstr);
function parsetree($treecache,$rootstr)
{
$tempstr = "";
$temp = explode(',',$rootstr);
foreach ($temp as $cid)
{
$info = $treecache[$cid];
$cidstr = $info['cid'];
$tempstr .= str_repeat('-',($info['depth']-1)*3);
$tempstr.=$info['title'];
if(empty($info['pid']))
{
//附加操作
}
$tempstr .= "<br/>";
if(!empty($info['cid']))
$tempstr .=parsetree($treecache,$info['cid']);
}
return $tempstr;
}
*/
?>
<?php
/**
* 功能: 根据条件建立分类缓存减少类别使用
* 创建日期:thu may 31 15:55:11 cst 2007
* 最后更新:
* 作者: sanshi <sanshi0815@tom.com>
*/
class treecache
{
var $tablename = "index_category"; //表名
var $where = "1"; //where条件
var $pidstr ="i_c_pid"; //pid 的字段名
var $tempcode = array(); //生成文件的数组
var $pid = '0'; //pid的初始值
var $db ; //数据库句柄
var $idstr="i_c_id"; //取得的数据id
var $title = "i_c_cn"; //名字字段
var $createarrayname = "treecache"; //建立的数组名字
var $createfilename =""; //建立文件的名字
var $appendarr = array(); //附加的属性,需要字段名与数据里的名对应
var $is_utf8 = false;
function treecache()
{
}
function set($db)
{
$this->db = $db;
$this->tempcode[] = "<?php";
}
//取得所有的一级
function getrootid()
{
$sql = "select {$this->idstr} from {$this->tablename} where
{$this->pidstr}='{$this->pid}' and {$this->where} ";
//exit($sql);
$result = $this->db->select($sql);
$temp = array();
foreach ($result as $r)
{
$temp[]=$r["{$this->idstr}"];
}
$this->tempcode[] = "\${$this->createarrayname}['root']='".implode(',',$temp)."';";
//print_r($temp);
return $temp;
}
//取得子id
function getchildren($pid)
{
$sql = "select {$this->idstr} from {$this->tablename} where
{$this->pidstr}='{$pid}' and {$this->where} ";
$result = $this->db->select($sql);
$temp = array();
foreach ($result as $r)
{
$temp[]=$r["{$this->idstr}"];
}
return $temp;
}
//取得夫id
function getparent($cid)
{
$sql = "select {$this->pidstr} from {$this->tablename} where
{$this->idstr}='{$cid}' and {$this->where} ";
$result = $this->db->select($sql);
//print_r($result);exit();
return $result[0]["{$this->pidstr}"];
}
//取得上级的id
function getpidstr($cid,$pidstr="")
{
$pid=$this->getparent($cid);
$temp = array();
while ($pid!=$this->pid && !empty($pid)) {
$temp[] = $pid;
$pid=$this->getparent($pid);
}
//print_r($temp);
return implode(',',$temp);
}
//取得深度
function getdepth($cid,$depth=0)
{
$pid=$this->getparent($cid);
$depth++;
if( $pid != $this->pid && !empty($pid))
$depth = $this->getdepth($pid,$depth);
return $depth;
}
//建立文件
function make()
{
if(empty($this->createfilename))
$this->createfilename = "{$this->createarrayname}.data.php";
$rootarr = $this->getrootid();
$selectf = "{$this->idstr},{$this->title},{$this->pidstr}";
foreach ($this->appendarr as $app)
{
if(empty($app)) continue;
$selectf .=",{$app}";
}
$sql = "select {$selectf} from {$this->tablename} where
{$this->where}";
$result = $this->db->select($sql);
for ($i=0;$i<count($result);$i++)
{
//id值
$this->tempcode[] =
"\${$this->createarrayname}['{$result[$i][$this->idstr]}']['id']='{$result[$i]["{$this->idstr}"]}';";
//标题
$this->tempcode[] =
"\${$this->createarrayname}['{$result[$i][$this->idstr]}']['title']='{$result[$i]["{$this->title}"]}';";
//父id
$this->tempcode[] =
"\${$this->createarrayname}['{$result[$i][$this->idstr]}']['pid']='{$result[$i]["{$this->pidstr}"]}';";
//子id
$this->tempcode[] =
"\${$this->createarrayname}['{$result[$i][$this->idstr]}']['cid']='".implode(',',$this->getchildren($result[$i]["$this->idstr"]))."';";
//目录深度
$this->tempcode[] =
"\${$this->createarrayname}['{$result[$i][$this->idstr]}']['depth']='".$this->getdepth($result[$i]["$this->idstr"])."';";
//父id的id串
$this->tempcode[] =
"\${$this->createarrayname}['{$result[$i][$this->idstr]}']['pstr']='".$this->getpidstr($result[$i]["$this->idstr"])."';";
//添加的附加属性
foreach ($this->appendarr as $app)
{
if(empty($app)) continue;
$this->tempcode[] =
"\${$this->createarrayname}['{$result[$i][$this->idstr]}']['{$app}']='{$result[$i]["{$app}"]}';";
}
}
$this->tempcode[] = "return \${$this->createarrayname};";
$this->tempcode[] = "?>";
//$content = implode("\n",$this->tempcode);
//print_r($this->tempcode);
$content = implode("\n",$this->tempcode);
//建立文件
$fio=factory::getbaseclass('fileio');
if($this->is_utf8) $content = "\xef\xbb\xbf".$content;
$fio->writefile($this->createfilename,$content);
return $content ;
}
}
//加载的文件就是为了,数据库连接
//需要数据库有 select 方法
/*
include_once(dirname(dirname(__file__))."/config/config.inc.php");
include_once(classes_path."factryobject.class.php");
$db = factryobject::getdb('indexpush');
$c = new treecache($db);
$c->make();
//exit();
//做分析
include_once("treecache.data.php");
$treecache=isset($treecache) ? $treecache : array();
$rootstr = isset($treecache['root']) ? $treecache['root'] : "";
echo parsetree($treecache,$rootstr);
function parsetree($treecache,$rootstr)
{
$tempstr = "";
$temp = explode(',',$rootstr);
foreach ($temp as $cid)
{
$info = $treecache[$cid];
$cidstr = $info['cid'];
$tempstr .= str_repeat('-',($info['depth']-1)*3);
$tempstr.=$info['title'];
if(empty($info['pid']))
{
//附加操作
}
$tempstr .= "<br/>";
if(!empty($info['cid']))
$tempstr .=parsetree($treecache,$info['cid']);
}
return $tempstr;
}
*/
?>
这个类是改善过的,当初做这个的适合,只能是数字的作为数组下标,这次支持字母了
另外就是关于解析
view plaincopy to clipboardprint?
<?php
class parsetree
{
var $ads_type_file = "";
var $isx = false;
var $rowsize=2;
function parsetree()
{
$this->ads_type_file = cache_path."ads_type_arr.data.php";
$this->ads_city_file = cache_path."ads_city_arr.data.php";
}
function make_ads_type()
{
$db = factory::getdb("ads_type");
$tree =factory::getitemclass('treecache');
$tree->set($db);
$tree->tablename=$db->tablename;
$tree->pidstr ="ads_type_pid";
$tree->idstr = "ads_type_id";
$tree->title = "ads_type_name";
$tree->createarrayname ="ads_type_arr";
$tree->where = " ads_type_state=1 order by ads_type_id desc ";
$tree->appendarr = array("ads_type_info");
$tree->createfilename = $this->ads_type_file;
$tree->is_utf8 = true;
return $tree->make();
}
function get_ads_type_str()
{
$temp_arr = $this->get_ads_type_arr();
$treearr = emptyempty($temp_arr) ? array() : $temp_arr;
$rootstr = isset($temp_arr['root']) ? $temp_arr['root'] : "";
$show_content = $this->__parsetree($treearr,$rootstr,'pares_type_link');
return $show_content;
}
function get_ads_type_arr()
{
return is_file($this->ads_type_file) ? require($this->ads_type_file) : array();
}
function pares_type_link($info)
{
$class_name = "ads_type";
$tempstr = "[<a href="?c={$class_name}&m=add&ads_type_pid={$info[" href="?c={$class_name}&m=add&ads_type_pid={$info["id']}'>子-添加</a>]";
$tempstr .="[<a href="?c={$class_name}&m=edit&ads_type_id={$info[" href="?c={$class_name}&m=edit&ads_type_id={$info["id']}'>编辑</a>]";
$tempstr .="[<a href="?c={$class_name}&m=del&ads_type_id={$info[" href="?c={$class_name}&m=del&ads_type_id={$info["id']}'>删除</a>]";
return $tempstr;
}
//根据提供的类别号取得类别数组
function get_type_arr($type_no=0)
{
$temp_arr = $this->get_ads_type_arr();
$rootstr = $type_no==0 ? (isset($temp_arr['root'])?$temp_arr['root'] : "") : (isset($temp_arr[$type_no]['cid'])?$temp_arr[$type_no]['cid']:"");
$temp = explode(',',$rootstr);
$return_temp = array();
foreach($temp as $cid)
{
if(isset($temp_arr[$cid])) $return_temp[$temp_arr[$cid]['id']]=$temp_arr[$cid]['title'];
}
return $return_temp;
}
function make_ads_city()
{
$db = factory::getdb("ads_city");
$tree =factory::getitemclass('treecache');
$tree->set($db);
$tree->tablename=$db->tablename;
$tree->pidstr ="ads_city_pid";
$tree->idstr = "ads_city_no";
$tree->title = "ads_city_name";
$tree->createarrayname ="ads_city_arr";
$tree->where = " ads_city_state=1 order by ads_city_no desc ";
$tree->appendarr = array("ads_city_info");
$tree->createfilename = $this->ads_city_file;
$tree->is_utf8 = true;
return $tree->make();
}
function get_ads_city_arr()
{
return is_file($this->ads_city_file) ? require($this->ads_city_file) : array();
}
function get_ads_city_str()
{
$temp_arr = $this->get_ads_city_arr();
$treearr = emptyempty($temp_arr) ? array() : $temp_arr;
$rootstr = isset($temp_arr['root']) ? $temp_arr['root'] : "";
$show_content = $this->__parsetree($treearr,$rootstr,'pares_city_link');
return $show_content;
}
function pares_city_link($info)
{
$class_name = "ads_city";
$tempstr = "[<a href="?c={$class_name}&m=add&ads_city_pid={$info[" href="?c={$class_name}&m=add&ads_city_pid={$info["id']}'>子-添加</a>]";
$tempstr .="[<a href="?c={$class_name}&m=edit&ads_city_no={$info[" href="?c={$class_name}&m=edit&ads_city_no={$info["id']}'>编辑</a>]";
$tempstr .="[<a href="?c={$class_name}&m=del&ads_city_no={$info[" href="?c={$class_name}&m=del&ads_city_no={$info["id']}'>删除</a>]";
return $tempstr;
}
//根据提供的城市号取得城市数组
function get_city_arr($city_no=0)
{
$temp_arr = $this->get_ads_city_arr();
$rootstr = $city_no==0 ? (isset($temp_arr['root'])?$temp_arr['root'] : "") : (isset($temp_arr[$city_no])?$temp_arr[$city_no]:"");
$temp = explode(',',$rootstr);
$return_temp = array();
foreach($temp as $cid)
{
if(isset($temp_arr[$cid])) $return_temp[$temp_arr[$cid]['id']]=$temp_arr[$cid]['title'];
}
return $return_temp;
}
function __parsetree($treecache,$rootstr,$fuc_str)
{
$tempstr = "";
$temp = explode(',',$rootstr);
if(emptyempty($temp)) return "";
$this->layer=0;
foreach ($temp as $cid)
{
if(isset($treecache[$cid]))
{
$info = $treecache[$cid];
$cidstr = $info['cid'];
//如果下面有子id
if($info["cid"]!="")
{
$tempstr .="<div >";
$tempstr .= str_repeat(' ',($info['depth']-1)*3);
if($info["cid"]!="")
{
//$tempstr .="<img src="/images/arrow.jpg" src="images/arrow.jpg" style="cursor:hand;" style="cursor:hand;" onclick='javascript:showmenu(\"l_{$info['id']}\");' id='pic_l_{$info['id']}'>";
}
//$tempstr .=">";
$tempstr.=$info['title'];
$tempstr .=$this->{$fuc_str}($info);
$tempstr .="</div>";
$tempstr .="<div id='l_{$info['id']}' >";
$tempstr .= $this->__parsetree($treecache,$info['cid'],$fuc_str);
$tempstr .="</div>";
}else{
$this->layer++;
$tempstr .= str_repeat(' ',($info['depth']-1)*3);
$tempstr.=$info['title'];
$tempstr .=$this->{$fuc_str}($info);
if($this->isx==true)
{
if($this->layer % $this->rowsize ==0)
$tempstr .= "<br/>";
else
$tempstr .= " ";
}else{
$tempstr .= "<br/>";
}
}
}
}
return $tempstr;
}
}
?>