<? Phpclass k_model_menu_menu {private $ data = array (); private $ rdata = array (); private $ jdata = array (); private $ level = 0; private $ paret = array (); function getOption ($ type = 'part', $ pid = 0) {if ($ type = 'all ') $ this-> data = R: getAll ('select * from menu '); $ this-> teamData (); if ($ this-> rdata) return $ this-> rdata; return false;} function addMenu ($ data) {foreach ($ data as $ key => $ value) {if ($ valu E = 'enter the content! ') $ Data [$ key] = '';} if ($ data) {$ menu = R: dispense ('menu '); $ menu-> pid = $ data ['pid ']; $ menu-> name = $ data ['name']; $ menu-> url = $ data ['url']; $ menu-> icon = $ data ['icon ']; $ id = R: store ($ menu ); return $ id ;}// return the json string public function getJsonMenu () {$ data = $ this-> getChild (1); $ this-> jdata = $ data; $ this-> recursive ($ this-> jdata); return json_encode ($ this-> jdata);} // The recursive function continuously generates subnodes and uses references, feeling This leads Use is like a magic pen, otherwise the implementation is too complicated public function recursive (& $ data = array () {foreach ($ data as $ key => $ value) {$ data [$ key] ['children '] = $ this-> getChild ($ value ['id']); $ tmp = & $ data [$ key] ['children ']; if ($ tmp) {$ this-> recursive ($ tmp) ;}}// organizes data, the select statement used to generate a tree structure returns an array // in the form of public function teamData ($ pid = 1) {foreach ($ this-> data as $ key => $ value) {if ($ value ['pid '] ==$ pid) {$ this-> level ++; array_push ($ this-> rd Ata, array ('name' => $ value ['name'], 'level' => $ this-> level, 'id' => $ value ['id']); $ tmpdata = $ this-> teamData ($ value ['id']); if (! $ Tmpdata) {$ this-> level --; continue ;}}// obtain the following sub-data public function getChild ($ pid) based on the pid {$ data = R :: getAll ("select * from menu where pid = {$ pid}"); $ tmpdata = array (); if ($ data) {foreach ($ data as $ key => $ value) {$ tmpdata [$ key] ['id'] = $ value ['id']; $ tmpdata [$ key] ['icon '] = $ value ['icon']; $ tmpdata [$ key] ['text'] = $ value ['name']; $ tmpdata [$ key] ['url'] = $ value ['url']; $ tmpdata [$ key] ['children '] = array ();}} return $ tmpdata ;}}