PHP Unlimited cache class extension _php tips

Source: Internet
Author: User
Tags explode

Copy Code code as follows:

<?php
/**
* Function: According to the conditions of the classification of the cache to reduce the use of categories
* Date Created: Thu May 15:55:11 CST 2007
* Last update:
* Author: Sanshi <sanshi0815@tom.com>
*/

Class Treecache
{
var $tableName = "Index_category"; Table name
var $where = "1"; Where condition
var $pidStr = "I_c_pid"; Field name of PID
var $tempCode = array (); Array of generated files
var $pid = ' 0 '; The initial value of the PID
var $db; Database handle
var $idStr = "i_c_id"; The Data ID obtained
var $title = "I_C_CN"; Name field
var $createArrayName = "Treecache"; Established array name
var $createFileName = ""; Create the name of the file
var $appendArr = array (); Additional attributes that require the field name to correspond to the name in the data
var $is _utf8 = false;
function Treecache ()
{
}
function set ($DB)
{
$this->db = $db;
$this->tempcode[] = "<?php";
}
Access to all levels
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;
}
Get Child 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;
}
Get a husband 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}"];
}
Get the ID of the superior
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);
}
Get the depth
function getdepth ($cid, $depth =0)
{
$pid = $this->getparent ($cid);
$depth + +;
if ($pid!= $this->pid &&!emptyempty ($pid))
$depth = $this->getdepth ($pid, $depth);
return $depth;
}
Create a file
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 value
$this->tempcode[] =
"\${$this->createarrayname}[' {$result [$i] [$this->idstr]} '] [' id ']= ' {$result [$i] [' {$this->idstr} ']} ';";
Title
$this->tempcode[] =
"\${$this->createarrayname}[' {$result [$i] [$this->idstr]} '] [' title ']= ' {$result [$i] [' {$this->title} ']} ' ;";
Parent ID
$this->tempcode[] =
"\${$this->createarrayname}[' {$result [$i] [$this->idstr]} '] [' pid ']= ' {$result [$i] [' {$this->pidstr} ']} '; ';
Child ID
$this->tempcode[] =
"\${$this->createarrayname}[' {$result [$i] [$this->idstr]} ' [' CID ']= ']. Implode (', ', $this->getchildren ($ result[$i] ["$this->idstr"]). "'; ';
Directory depth
$this->tempcode[] =
"\${$this->createarrayname}[' {$result [$i] [$this->idstr]} '] [' depth ']= ']. $this->getdepth ($result [$i] [" $ This->idstr "])." '; ';
ID string for parent ID
$this->tempcode[] =
"\${$this->createarrayname}[' {$result [$i] [$this->idstr]} '] [' pstr ']= ']. $this->getpidstr ($result [$i] [" $ This->idstr "])." '; ';
Additional properties added
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);
Create a file
$fio =factory::getbaseclass (' FileIO ');
if ($this->is_utf8) $content = "\XEF\XBB\XBF". $content;
$fio->writefile ($this->createfilename, $content);
return $content;
}
}
The file is loaded so that the database is connected
Need a database with a Select method
/*
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 ();

Do analysis
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 '])
{
Additional actions
}
$tempStr. = "<br/>";
if (!empty ($info [' CID '])
$tempStr. =parsetree ($treeCache, $info [' CID ']);
}
return $tempStr;
}
*/
?>
<?php
/**
* Function: According to the conditions of the classification of the cache to reduce the use of categories
* Date Created: Thu May 15:55:11 CST 2007
* Last update:
* Author: Sanshi <sanshi0815@tom.com>
*/

Class Treecache
{
var $tableName = "Index_category"; Table name
var $where = "1"; Where condition
var $pidStr = "I_c_pid"; Field name of PID
var $tempCode = array (); Array of generated files
var $pid = ' 0 '; The initial value of the PID
var $db; Database handle
var $idStr = "i_c_id"; The Data ID obtained
var $title = "I_C_CN"; Name field
var $createArrayName = "Treecache"; Established array name
var $createFileName = ""; Create the name of the file
var $appendArr = array (); Additional attributes that require the field name to correspond to the name in the data
var $is _utf8 = false;
function Treecache ()
{
}
function set ($DB)
{
$this->db = $db;
$this->tempcode[] = "<?php";
}
Access to all levels
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;
}
Get Child 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;
}
Get a husband 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}"];
}
Get the ID of the superior
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);
}
Get the depth
function getdepth ($cid, $depth =0)
{
$pid = $this->getparent ($cid);
$depth + +;
if ($pid!= $this->pid &&!empty ($pid))
$depth = $this->getdepth ($pid, $depth);
return $depth;
}
Create a file
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 value
$this->tempcode[] =
"\${$this->createarrayname}[' {$result [$i] [$this->idstr]} '] [' id ']= ' {$result [$i] [' {$this->idstr} ']} ';";
Title
$this->tempcode[] =
"\${$this->createarrayname}[' {$result [$i] [$this->idstr]} '] [' title ']= ' {$result [$i] [' {$this->title} ']} ' ;";
Parent ID
$this->tempcode[] =
"\${$this->createarrayname}[' {$result [$i] [$this->idstr]} '] [' pid ']= ' {$result [$i] [' {$this->pidstr} ']} '; ';
Child ID
$this->tempcode[] =
"\${$this->createarrayname}[' {$result [$i] [$this->idstr]} ' [' CID ']= ']. Implode (', ', $this->getchildren ($ result[$i] ["$this->idstr"]). "'; ';
Directory depth
$this->tempcode[] =
"\${$this->createarrayname}[' {$result [$i] [$this->idstr]} '] [' depth ']= ']. $this->getdepth ($result [$i] [" $ This->idstr "])." '; ';
ID string for parent ID
$this->tempcode[] =
"\${$this->createarrayname}[' {$result [$i] [$this->idstr]} '] [' pstr ']= ']. $this->getpidstr ($result [$i] [" $ This->idstr "])." '; ';
Additional properties added
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);
Create a file
$fio =factory::getbaseclass (' FileIO ');
if ($this->is_utf8) $content = "\XEF\XBB\XBF". $content;
$fio->writefile ($this->createfilename, $content);
return $content;
}
}
The file is loaded so that the database is connected
Need a database with a Select method
/*
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 ();

Do analysis
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 '])
{
Additional actions
}
$tempStr. = "<br/>";
if (!empty ($info [' CID '])
$tempStr. =parsetree ($treeCache, $info [' CID ']);
}
return $tempStr;
}
*/
?>

This class is improved, the original to do this fit, can only be a number of arrays as subscript, this time to support the letter

The other is about parsing

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 ']} ' > Child-Add </a>]";
$tempStr. = "[<a href="? c={$class _name}&m=edit&ads_type_id={$info ["href=" c={$class _name}&m=edit &ads_type_id={$info ["id ']} ' > Edit </a>]";
$tempStr. = "[<a href="? c={$class _name}&m=del&ads_type_id={$info ["href=" c={$class _name}&m=del &ads_type_id={$info ["id ']} ' > Delete </a>]";
return $tempStr;
}
Gets the category array according to the provided category number
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 ']} ' > Child-Add </a>]";
$tempStr. = "[<a href="? c={$class _name}&m=edit&ads_city_no={$info ["href=" c={$class _name}&m=edit &ads_city_no={$info ["id ']} ' > Edit </a>]";
$tempStr. = "[<a href="? c={$class _name}&m=del&ads_city_no={$info ["href=" c={$class _name}&m=del &ads_city_no={$info ["id ']} ' > Delete </a>]";
return $tempStr;
}
Get the city array according to the city number provided
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 '];
If you have a child ID below
if ($info ["CID"]!= "")
{
$tempStr. = "<div >";
$tempStr. = Str_repeat ("", ($info [' depth ']-1) *3);
if ($info ["CID"]!= "")
{
$tempStr. = " ';
}
$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;
}
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.