Create Category cache based on conditions to reduce category usage
The code is as follows:
/**
* 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:
* Author: sanshi
*/
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 [] =" }
// 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 {
// 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. ="
";
If (! Empty ($ info ['CID'])
$ TempStr. = parseTree ($ treeCache, $ info ['CID']);
}
Return $ tempStr;
}
*/
?>
/**
* 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:
* Author: sanshi
*/
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 [] =" }
// 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 {
// 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. ="
";
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?
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;
}
// 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 = "[sub-add]";
$ TempStr. = "[edit]";
$ TempStr. = "[delete]";
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. ="
";
$ TempStr. = str_repeat ('', ($ info ['depth ']-1) * 3 );
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) * 3 );
$ 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;
}
}
?>