Provides a variety of official and user-released code examples. For code reference, you are welcome to exchange and learn the basic idea is to first combine the columns into multi-dimensional arrays recursively and cache them. Multi-dimensional arrays represent first-level ones, and then output the multi-dimensional arrays in a recursive way during output. In addition, I wrote JQUERY code to expand and close subcategories.
The day before yesterday, my brother wrote a recursion, found that the number of database queries too many, so modified a bit, now only check the database once, the problem of sending me mail exchange 576272452@qq.com, no code no truth, the following code
1. Generate a cache file when the column is modified and added successfully, and use the cache in the template
/* Update the cache file */
$ Map ['topid'] = 0;
$ Map ['yiyunid'] = $ this-> _ get ('yiyunid ');
$ Other ['order'] = 'sort desc, keshi. id asc ';
$ Other ['field'] = 'keshi. *, yiyuan. name as yiyuan ';
$ Other ['join'] = 'yiyuan on yiyuan. id = keshi. yiyuanid ';
$ Digui = digui ('keshi', $ map, $ other );
S ('keshi', $ digui); // cache data
2. Three core recursive functions
/*
* Recursive data is recursively converted into multi-dimensional arrays. childs stores lower-level data. In the production environment, cache the returned values so that the database can be queried only once, you can call this function to update the cache in the newly added and updated methods.
* @ Param string $ name of the table to be queried
* @ Param array $ where condition for where Query
* @ Param array $ other variables required for the other Query
* @ Return array $ list a multi-dimensional array representing hierarchical relationships
*/
Function digui ($ table, $ where, $ other ){
$ Form = M ($ table );
If ($ other ['join']) {
$ List = $ Form-> field ($ other ['field'])-> join ($ other ['join'])-> where ($ where) -> order ($ other ['order'])-> select ();
} Else {
$ List = $ Form-> field ($ other ['field'])-> where ($ where)-> order ($ other ['order']) -> select ();
}
Foreach ($ list as $ k => $ v ){
If ($ digui = digui ($ table, array ('topid '=> $ v ['id']), $ other )){
$ List [$ k] ['childs'] = $ digui; // recursive subclass
}
}
Return $ list;
}
/*
* The function that reads recursive data. cache the return value of the digui () function before use.
* @ Param array $ list this parameter is a multi-dimensional array of hierarchical relationships read by S ('keshi'), produced by digui ().
* @ Return void: Direct output without return value
*/
Function KeshiType ($ list ){
Foreach ($ list as $ k => $ v ){
Echo''. $ V ['id'].''. $ V ['yiyuanyuan'].''. $ V ['name'].'Edit edit Delete';
KeshiTypeSeed ($ v ['childs ']);
}
}
/*
* The subfunctions of the upstairs KeshiType work with recursion
*/
Function KeshiTypeSeed ($ list ){
If ($ list ){
Echo'
';Foreach ($ list as $ k => $ v ){Echo'
'. $ V ['id'].' |
'. $ V ['yiyuanyuan'].' |
'. $ V ['name'].' |
Edit Management 2 manage Delete |
';KeshiTypeSeed ($ v ['childs ']);}Echo'
';
}
}
3. Use cache in the template for Recursive Columns
/I added the jquery code for clicking to expand and close the column,
Script
/*
* Click to expand subclass
*
*/
$ (". Jia"). click (function (){
If ($ (this). parent ("td"). parent ("tr"). next (". seed"). is (": hidden ")){
$ (This). parent ("td"). parent ("tr"). next (". seed"). show ();
Certificate (this).html ("-");
} Else {
$ (This). parent ("td"). parent ("tr"). next (". seed"). hide ();
Certificate (this).html ("+ ");
}
});
$ (". Seed"). prev (". fid"). find ("td"). find ("span. jia" 2.16.html ("-");
/*
* Click to close all subclasses.
*
*/
$ ("# Closeed"). click (function (){
If ($ (this). val () = "show all subclasses "){
$ (". Seed"). show ();
$ (This). val ("close all subclasses ");
$. Each ($ (". jia"), function (k, v ){
If(((this).html ()){
Certificate (this).html ("-");
}
});
} Else {
$ (". Seed"). hide ();
$ (This). val ("show all subclasses ");
$. Each ($ (". jia"), function (k, v ){
If(((this).html ()){
Certificate (this).html ("+ ");
}
});
}
});
});
Script
// Recursive part of the template
$ Keshis = S ('keshi'); // read the cache
KeshiType ($ keshis); // recursively Outputs Data
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB