PHP Infinite Cache class Extension _php tutorial

Source: Internet
Author: User
Copy CodeThe code is as follows:
/**
* Function: Set up classification cache according to condition reduce category use
* Date Created: Thu 15:55:11 CST 2007
* Last update:
* Author: Sanshi
*/

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 (); An 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"; Created array name
var $createFileName = ""; Establish 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[] = " }
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 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);
}
Gain 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 {
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 loaded file is for the database connection
Requires a database with 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)];
$tempStr. = $info [' title '];
if (Empty ($info [' pid ']))
{
Additional actions
}
$tempStr. = "
";
if (!empty ($info [' CID ']))
$tempStr. =parsetree ($treeCache, $info [' CID ']);
}
return $tempStr;
}
*/
?>
/**
* Function: Set up classification cache according to condition reduce category use
* Date Created: Thu 15:55:11 CST 2007
* Last update:
* Author: Sanshi
*/

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 (); An 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"; Created array name
var $createFileName = ""; Establish 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[] = " }
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 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);
}
Gain 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 {
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 loaded file is for the database connection
Requires a database with 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)];
$tempStr. = $info [' title '];
if (Empty ($info [' pid ']))
{
Additional actions
}
$tempStr. = "
";
if (!empty ($info [' CID ']))
$tempStr. =parsetree ($treeCache, $info [' CID ']);
}
return $tempStr;
}
*/
?>

This class is improved, the original to do this fit, only the number as an array subscript, this time to support the letter

The other is about parsing

View Plaincopy to Clipboardprint?
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 = "[Sub-add]";
$tempStr. = "[Edit]";
$tempStr. = "[Delete]";
return $tempStr;
}
Get the category array based on the category number provided
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 = "[Sub-add]";
$tempStr. = "[Edit]";
$tempStr. = "[Delete]";
return $tempStr;
}
Get an array of cities based on 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 there is a child ID below
if ($info ["CID"]!= "")
{
$tempStr. = "";
$tempStr. = Str_repeat (', ($info [' depth ']-1)];
if ($info ["CID"]!= "")
{
$tempStr. = "";
}
$tempStr. = ">";
$tempStr. = $info [' title '];
$tempStr. = $this->{$fuc _str} ($info);
$tempStr. = "";
$tempStr. = "";
$tempStr. = $this->__parsetree ($treeCache, $info [' CID '], $fuc _str);
$tempStr. = "";
}else{
$this->layer++;
$tempStr. = Str_repeat (', ($info [' depth ']-1)];
$tempStr. = $info [' title '];
$tempStr. = $this->{$fuc _str} ($info);
if ($this->isx==true)
{
if ($this->layer% $this->rowsize ==0)
$tempStr. = "
";
Else
$tempStr. = "";
}else{
$tempStr. = "
";
}
}
}
}
return $tempStr;
}
}
?>

http://www.bkjia.com/PHPjc/319904.html www.bkjia.com true http://www.bkjia.com/PHPjc/319904.html techarticle Copy the code as follows: PHP/** * Function: Set up classification cache based on criteria reduce category use * Date Created: Thu 15:55:11 CST 2007 * Last Updated: * Author: Sanshi sanshi08 ...

  • 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.