Php unlimited cache class Extension

Source: Internet
Author: User

Copy codeThe Code is as follows: <? Php
/**
* Function: Create a category based on the condition. cache is used to reduce the category.
* Creation date: Thu May 31 15:55:11 CST 2007
* Last Update:
* By sanshi <sanshi0815@tom.com>
*/

Class treeCache
{
Var $ tableName = "index_category"; // table name
Var $ where = "1"; // where Condition
Var $ pidStr = "I _c_pid"; // The field name of the pid.
Var $ tempCode = array (); // array of generated files
Var $ pid = '0'; // initial value of the pid
Var $ db; // database handle
Var $ idStr = "I _c_id"; // the id of the retrieved data.
Var $ title = "I _c_cn"; // Name field
Var $ createArrayName = "treeCache"; // The Name Of The created array.
Var $ createFileName = ""; // name of the created File
Var $ appendArr = array (); // additional attributes. The field name must correspond to the name in the data.
Var $ is_utf8 = false;
Function treeCache ()
{
}
Function set ($ db)
{
$ This-> db = $ db;
$ This-> tempCode [] = "<? Php ";
}
// Obtain all level 1
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;
}
// Obtain the subid
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;
}
// Obtain the 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}"];
}
// Obtain the superior 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 );
}
// Obtain 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}"]} '; ";
// Subid
$ 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]}'] ['dest'] = '". $ this-> getDepth ($ result [$ I] ["$ this-> idStr"]). "';";
// Id string of the parent id
$ This-> tempCode [] =
"\ $ {$ This-> createArrayName} ['{$ result [$ I] [$ this-> idStr]}'] ['pstr'] = '". $ this-> getPidStr ($ result [$ I] ["$ this-> idStr"]). "';";
// Added additional attributes
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 to be loaded is for database connection
// The select method is required for the database
/*
Include_once (dirname (_ FILE _). "/config. inc. php ");
Include_once (CLASSES_PATH. "factryObject. class. php ");

$ Db = factryObject: getDB ('indexpush ');
$ C = new treeCache ($ db );
$ C-> make ();
// Exit ();

// Perform 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 operations
}
$ TempStr. = "<br/> ";
If (! Empty ($ info ['cid'])
$ TempStr. = parseTree ($ treeCache, $ info ['cid']);
}
Return $ tempStr;
}
*/
?>
<? Php
/**
* Function: Create a category based on the condition. cache is used to reduce the category.
* Creation date: Thu May 31 15:55:11 CST 2007
* Last Update:
* By sanshi <sanshi0815@tom.com>
*/

Class treeCache
{
Var $ tableName = "index_category"; // table name
Var $ where = "1"; // where Condition
Var $ pidStr = "I _c_pid"; // The field name of the pid.
Var $ tempCode = array (); // array of generated files
Var $ pid = '0'; // initial value of the pid
Var $ db; // database handle
Var $ idStr = "I _c_id"; // the id of the retrieved data.
Var $ title = "I _c_cn"; // Name field
Var $ createArrayName = "treeCache"; // The Name Of The created array.
Var $ createFileName = ""; // name of the created File
Var $ appendArr = array (); // additional attributes. The field name must correspond to the name in the data.
Var $ is_utf8 = false;
Function treeCache ()
{
}
Function set ($ db)
{
$ This-> db = $ db;
$ This-> tempCode [] = "<? Php ";
}
// Obtain all level 1
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;
}
// Obtain the subid
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;
}
// Obtain the 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}"];
}
// Obtain the superior 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 );
}
// Obtain 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}"]} '; ";
// Subid
$ 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]}'] ['dest'] = '". $ this-> getDepth ($ result [$ I] ["$ this-> idStr"]). "';";
// Id string of the parent id
$ This-> tempCode [] =
"\ $ {$ This-> createArrayName} ['{$ result [$ I] [$ this-> idStr]}'] ['pstr'] = '". $ this-> getPidStr ($ result [$ I] ["$ this-> idStr"]). "';";
// Added additional attributes
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 to be loaded is for database connection
// The select method is required for the database
/*
Include_once (dirname (_ FILE _). "/config. inc. php ");
Include_once (CLASSES_PATH. "factryObject. class. php ");

$ Db = factryObject: getDB ('indexpush ');
$ C = new treeCache ($ db );
$ C-> make ();
// Exit ();

// Perform 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 operations
}
$ TempStr. = "<br/> ";
If (! Empty ($ info ['cid'])
$ TempStr. = parseTree ($ treeCache, $ info ['cid']);
}
Return $ tempStr;
}
*/
?>

This class has been improved. At the beginning, it was appropriate to use numbers as array subscripts. This time, letters are supported.

It 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']} '> sub-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;
}
// Obtain a category Array Based on 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']} '> sub-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;
}
// Obtain the city Array Based on the provided city number
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 subid
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.