About the extension of Infinitely cached classes

Source: Internet
Author: User

<? Php <br/>/** <br/> * function: you can set a category based on the condition. cache is used to reduce the category. <br/> * creation date: thu May 31 15:55:11 CST 2007 <br/> * last updated: <br/> * Author: sanshi <sanshi0815@tom.com> <br/> */</P> <p> class treecache <br/> {<br/> var $ tablename = "index_category "; // table name <br/> var $ where = "1"; // Where condition <br/> var $ pidstr = "I _c_pid "; // PID field name <br/> var $ tempcode = array (); // array of generated files <br/> var $ pid = '0 '; // initial PID value <br/> var $ db; // database handle <br/> var $ IDS Tr = "I _c_id"; // the obtained data id <br/> var $ Title = "I _c_cn"; // Name field <br/> var $ createarrayname = "treecache "; // create an array name <br/> var $ createfilename = ""; // create a file name <br/> var $ appendarr = array (); // additional attribute. The field name must correspond to the name in the Data <br/> var $ is_utf8 = false; <br/> function treecache () <br/>{< br/>}< br/> function set ($ dB) <br/>{< br/> $ this-> DB = $ dB; <br/> $ this-> tempcode [] = "<? PHP "; <br/>}< br/> // obtain all level 1 functions. <br/> function getrootid () <br/>{< br/> $ SQL = "select {$ this-> idstr} from {$ this-> tablename} Where <br/>{$ this-> pidstr} = '{$ this-> PID}' and {$ this-> where }"; <br/> // exit ($ SQL); <br/> $ result = $ this-> DB-> select ($ SQL ); <br/> $ temp = array (); <br/> foreach ($ result as $ R) <br/>{< br/> $ temp [] = $ R ["{$ this-> idstr}"]; <br/>}< br/> $ this-> tempcode [] = "/$ {$ this-> createarrayname} ['root '] = '". Implode (',', $ temp ). "';"; <br/> // print_r ($ temp); <br/> return $ temp; <br/>}< br/> // obtain the subid <br/> function getchildren ($ PID) <br/>{< br/> $ SQL = "select {$ this-> idstr} from {$ this-> tablename} Where <br/>{$ this-> pidstr} = '{$ PID}' and {$ this-> where }"; <br/> $ result = $ this-> DB-> select ($ SQL); <br/> $ temp = array (); <br/> foreach ($ result as $ R) <br/>{< br/> $ temp [] = $ R ["{$ this-> idstr}"]; <br/>}< br/> return $ Temp; <br/>}< br/> // obtain the husband ID <br/> function getparent ($ CID) <br/>{< br/> $ SQL = "select {$ this-> pidstr} from {$ this-> tablename} Where <br/>{$ this-> idstr} = '{$ CID}' and {$ this-> where }"; <br/> $ result = $ this-> DB-> select ($ SQL); <br/> // print_r ($ result); exit (); <br/> return $ result [0] ["{$ this-> pidstr}"]; <br/>}< br/> // obtain the parent id <br/> function getpidstr ($ CID, $ pidstr = "") <br/>{< br/> $ pid = $ this-> getparent ($ CID ); <Br/> $ temp = array (); <br/> while ($ PID! = $ This-> PID &&! Empty ($ PID) {<br/> $ temp [] = $ PID; <br/> $ pid = $ this-> getparent ($ PID ); <br/>}< br/> // print_r ($ temp); <br/> return implode (',', $ temp ); <br/>}< br/> // obtain the depth <br/> function getdepth ($ CID, $ depth = 0) <br/>{< br/> $ pid = $ this-> getparent ($ CID); <br/> $ depth ++; <br/> if ($ PID! = $ This-> PID &&! Empty ($ PID) <br/> $ depth = $ this-> getdepth ($ PID, $ depth); <br/> return $ depth; <br/>}< br/> // create a file <br/> function make () <br/>{< br/> If (empty ($ this-> createfilename) <br/> $ this-> createfilename = "{$ this-> createarrayname }. data. PHP "; </P> <p> $ rootarr = $ this-> getrootid (); <br/> $ selectf =" {$ this-> idstr }, {$ this-> title}, {$ this-> pidstr} "; <br/> foreach ($ this-> appendarr as $ APP) <br/> {<br/> If (empty ($ APP) con Tinue; <br/> $ selectf. = ", {$ app }"; <br/>}< br/> $ SQL = "select {$ selectf} from {$ this-> tablename} Where <br/>{$ this-> where }"; <br/> $ result = $ this-> DB-> select ($ SQL); <br/> for ($ I = 0; $ I <count ($ result ); $ I ++) <br/>{< br/> // id value <br/> $ this-> tempcode [] = <br/> "/$ {$ this-> createarrayname }[ '{$ result [$ I] [$ this-> idstr]}'] ['id'] = '{$ result [$ I] ["{$ this-> idstr} "]} '; "; <br/> // Title <br/> $ this-> tempcode [] = <br/>"/$ {$ th Is-> createarrayname} ['{$ result [$ I] [$ this-> idstr]}'] ['title'] = '{$ result [$ I] [" {$ this-> title} "]} '; "; <br/> // parent id <br/> $ this-> tempcode [] = <br/> "/$ {$ this-> createarrayname} ['{$ result [$ I] [$ this-> idstr]} '] ['pid'] = '{$ result [$ I] ["{$ this-> pidstr}"]}'; "; <br/> // subid <br/> $ this-> tempcode [] = <br/> "/$ {$ this-> createarrayname} ['{$ result [$ I] [$ this-> idstr]} '] ['cid'] = '". implode (',', $ this-> getchildren ($ result [$ I] ["$ th Is-> idstr "]). "';"; <br/> // directory depth <br/> $ this-> tempcode [] = <br/> "/$ {$ this-> createarrayname} ['{$ result [$ I] [$ this-> idstr]} '] ['depth'] = '". $ this-> getdepth ($ result [$ I] ["$ this-> idstr"]). "';"; <br/> // ID string of the parent id <br/> $ this-> tempcode [] = <br/> "/$ {$ this-> createarrayname }['{ $ result [$ I] [$ this-> idstr]} '] ['pstr'] = '". $ this-> getpidstr ($ result [$ I] ["$ this-> idstr"]). "';"; <br/> // added additional attributes <br/> foreach ($ this-> appendarr $ APP) <br/>{< br/> If (empty ($ APP) continue; <br/> $ this-> tempcode [] = <br/> "/$ {$ this-> createarrayname} ['{$ result [$ I] [$ this-> idstr]} '] [' {$ app} '] =' {$ result [$ I] ["{$ app}"]} '; "; <br/>}< br/> $ this-> tempcode [] =" return/$ {$ this-> createarrayname };"; <br/> $ this-> tempcode [] = "?> "; <Br/> // $ content = implode ("/N ", $ this-> tempcode); <br/> // print_r ($ this-> tempcode ); <br/> $ content = implode ("/N", $ this-> tempcode); <br/> // create a file <br/> $ FIO = Factory :: getbaseclass ('fileio'); <br/> if ($ this-> is_utf8) $ content = "/XeF/xbb/xbf ". $ content; <br/> $ FIO-> writefile ($ this-> createfilename, $ content); <br/> return $ content; <br/>}< br/> // The file is loaded, database Connection <br/> // requires the select method for the database <br/>/* <br/> Inc Lude_once (dirname (_ file __)). "/config. inc. PHP "); <br/> include_once (classes_path. "factryobject. class. PHP "); </P> <p> $ db = factryobject: getdb ('indexpush'); <br/> $ c = new treecache ($ dB ); <br/> $ C-> make (); <br/> // exit (); </P> <p> // perform analysis <br/> include_once ("treecache. data. PHP "); <br/> $ treecache = isset ($ treecache )? $ Treecache: array (); <br/> $ rootstr = isset ($ treecache ['root'])? $ Treecache ['root']: ""; <br/> echo parsetree ($ treecache, $ rootstr); <br/> function parsetree ($ treecache, $ rootstr) <br/>{< br/> $ tempstr = ""; <br/> $ temp = explode (',', $ rootstr ); <br/> foreach ($ temp as $ CID) <br/>{< br/> $ info = $ treecache [$ CID]; <br/> $ cidstr = $ info ['cid']; <br/> $ tempstr. = str_repeat ('-', ($ info ['depth ']-1) * 3); <br/> $ tempstr. = $ info ['title']; <br/> If (empty ($ info ['pid ']) <br/>{< br/> // Additional Operations <br/>}< br/> $ tempstr. = "<br/>"; <br/> If (! Empty ($ info ['cid']) <br/> $ tempstr. = parsetree ($ treecache, $ info ['cid']); <br/>}< br/> return $ tempstr; <br/>}< br/> */<br/>?>

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

<? Php <br/> class parsetree <br/> {<br/> var $ ads_type_file = ""; <br/> var $ ISX = false; <br/> var $ rowsize = 2; <br/> function parsetree () <br/>{< br/> $ this-> ads_type_file = cache_path. "ads_type_arr.data.php"; <br/> $ this-> ads_city_file = cache_path. "ads_city_arr.data.php"; <br/>}< br/> function make_ads_type () <br/>{< br/> $ db = Factory: getdb ("ads_type "); <br/> $ tree = Factory: getitemclass ('treecache'); <br /> $ Tree-> set ($ dB); <br/> $ tree-> tablename = $ db-> tablename; <br/> $ tree-> pidstr = "ads_type_pid"; <br/> $ tree-> idstr = "ads_type_id "; <br/> $ tree-> Title = "ads_type_name"; <br/> $ tree-> createarrayname = "ads_type_arr "; <br/> $ tree-> where = "ads_type_state = 1 order by ads_type_id DESC"; <br/> $ tree-> appendarr = array ("ads_type_info "); <br/> $ tree-> createfilename = $ this-> ads_type_file; <br/> $ tree-> is_utf8 = T Rue; <br/> return $ tree-> make (); <br/>}< br/> function get_ads_type_str () <br/>{< br/> $ temp_arr = $ this-> get_ads_type_arr (); <br/> $ treearr = empty ($ temp_arr )? Array (): $ temp_arr; <br/> $ rootstr = isset ($ temp_arr ['root'])? $ Temp_arr ['root']: ""; <br/> $ show_content = $ this->__ parsetree ($ treearr, $ rootstr, 'pares _ type_link '); <br/> return $ show_content; <br/>}< br/> function get_ads_type_arr () <br/>{< br/> return is_file ($ this-> ads_type_file )? Require ($ this-> ads_type_file): array (); <br/>}< br/> function pares_type_link ($ info) <br/>{< br/> $ class_name = "ads_type"; <br/> $ tempstr = "[<a href = "? C = {$ class_name} & M = add & ads_type_pid = {$ info ["mce_href = "? C = {$ class_name} & M = add & ads_type_pid = {$ info ["id']} '> sub-add </a>]"; <br/> $ tempstr. = "[<a href = "? C = {$ class_name} & M = edit & ads_type_id = {$ info ["mce_href = "? C = {$ class_name} & M = edit & ads_type_id = {$ info ["id']} '> edit </a>]"; <br/> $ tempstr. = "[<a href = "? C = {$ class_name} & M = del & ads_type_id = {$ info ["mce_href = "? C = {$ class_name} & M = del & ads_type_id = {$ info ["id']} '> Delete </a>]"; <br/> return $ tempstr; <br/>}< br/> // obtain a category Array Based on the provided category number <br/> function get_type_arr ($ type_no = 0) <br/>{< br/> $ temp_arr = $ this-> get_ads_type_arr (); <br/> $ rootstr = $ type_no = 0? (Isset ($ temp_arr ['root'])? $ Temp_arr ['root']: ""): (isset ($ temp_arr [$ type_no] ['cid'])? $ Temp_arr [$ type_no] ['cid']: ""); <br/> $ temp = explode (',', $ rootstr ); <br/> $ return_temp = array (); <br/> foreach ($ temp as $ CID) <br/>{< br/> If (isset ($ temp_arr [$ CID]) $ return_temp [$ temp_arr [$ CID] ['id'] = $ temp_arr [$ CID] ['title']; <br/>}< br/> return $ return_temp; <br/>}</P> <p> function make_ads_city () <br/>{< br/> $ db = Factory: getdb ("ads_city"); <br/> $ tree = Factory: getitemclass ('treecache '); <br/> $ tree -> Set ($ dB); <br/> $ tree-> tablename = $ db-> tablename; <br/> $ tree-> pidstr = "ads_city_pid "; <br/> $ tree-> idstr = "ads_city_no"; <br/> $ tree-> Title = "ads_city_name "; <br/> $ tree-> createarrayname = "ads_city_arr"; <br/> $ tree-> where = "ads_city_state = 1 order by ads_city_no DESC "; <br/> $ tree-> appendarr = array ("ads_city_info"); <br/> $ tree-> createfilename = $ this-> ads_city_file; <br/> $ tree-> is_utf8 = true; <br /> Return $ tree-> make (); <br/>}< br/> function get_ads_city_arr () <br/>{< br/> return is_file ($ this-> ads_city_file )? Require ($ this-> ads_city_file): array (); <br/>}< br/> function get_ads_city_str () <br/>{< br/> $ temp_arr = $ this-> get_ads_city_arr (); <br/> $ treearr = empty ($ temp_arr )? Array (): $ temp_arr; <br/> $ rootstr = isset ($ temp_arr ['root'])? $ Temp_arr ['root']: ""; <br/> $ show_content = $ this->__ parsetree ($ treearr, $ rootstr, 'pares _ city_link '); <br/> return $ show_content; <br/>}< br/> function pares_city_link ($ info) <br/>{< br/> $ class_name = "ads_city "; <br/> $ tempstr = "[<a href = "? C = {$ class_name} & M = add & ads_city_pid = {$ info ["mce_href = "? C = {$ class_name} & M = add & ads_city_pid = {$ info ["id']} '> sub-add </a>]"; <br/> $ tempstr. = "[<a href = "? C = {$ class_name} & M = edit & ads_city_no = {$ info ["mce_href = "? C = {$ class_name} & M = edit & ads_city_no = {$ info ["id']} '> edit </a>]"; <br/> $ tempstr. = "[<a href = "? C = {$ class_name} & M = del & ads_city_no = {$ info ["mce_href = "? C = {$ class_name} & M = del & ads_city_no = {$ info ["id']} '> Delete </a>]"; <br/> return $ tempstr; <br/>}< br/> // obtain the city Array Based on the provided city number <br/> function get_city_arr ($ city_no = 0) <br/>{< br/> $ temp_arr = $ this-> get_ads_city_arr (); <br/> $ rootstr = $ city_no = 0? (Isset ($ temp_arr ['root'])? $ Temp_arr ['root']: ""): (isset ($ temp_arr [$ city_no])? $ Temp_arr [$ city_no]: ""); <br/> $ temp = explode (',', $ rootstr); <br/> $ return_temp = array (); <br/> foreach ($ temp as $ CID) <br/>{< br/> If (isset ($ temp_arr [$ CID]) $ return_temp [$ temp_arr [$ CID] ['id'] = $ temp_arr [$ CID] ['title']; <br/>}< br/> return $ return_temp; <br/>}</P> <p> function _ parsetree ($ treecache, $ rootstr, $ fuc_str) <br/>{< br/> $ tempstr = ""; <br/> $ temp = explode (',', $ rootstr ); <br/> If (empty ($ temp )) Return ""; <br/> $ this-> layer = 0; <br/> foreach ($ temp as $ CID) <br/>{< br/> If (isset ($ treecache [$ CID]) <br/>{< br/> $ info = $ treecache [$ CID]; <br/> $ cidstr = $ info ['cid']; <br/> // if there is a subid <br/> if ($ info ["CID"]! = "") <Br/>{< br/> $ tempstr. = "<div>"; <br/> $ tempstr. = str_repeat ('', ($ info ['depth ']-1) * 3); <br/> if ($ info [" CID "]! = "") <Br/>{< br/> // $ tempstr. = " "; <br/>}< br/> // $ tempstr. = ">"; <br/> $ tempstr. = $ info ['title']; <br/> $ tempstr. = $ this-> {$ fuc_str} ($ info); <br/> $ tempstr. = "</div>"; <br/> $ tempstr. = "<Div id = 'l _ {$ info ['id']} '>"; <br/> $ tempst R. = $ this->__ parsetree ($ treecache, $ info ['cid'], $ fuc_str); <br/> $ tempstr. = "</div>"; <br/>} else {<br/> $ this-> layer ++; <br/> $ tempstr. = str_repeat ('', ($ info ['depth ']-1) * 3); <br/> $ tempstr. = $ info ['title']; <br/> $ tempstr. = $ this-> {$ fuc_str} ($ info); <br/> if ($ this-> ISX = true) <br/>{< br/> if ($ this-> layer % $ this-> rowsize = 0) <br/> $ tempstr. = "<br/>"; <br/> else <br/> $ tempstr. = ""; <br/>}else {<br/> $ te Mpstr. = "<br/>"; <br/>}< br/> return $ tempstr; <br/>}< br/>?>

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.