This article mainly introduces ThinkPHP's use of volist to implement three-level cyclic code instances. It provides examples to help readers better understand the usage of volist labels. For more information, see the following example. The tag implements three-level cyclic code. the procedure is as follows:
1. a three-level loop requires a three-dimensional array. the implementation code is as follows:
Function MakeTree ($ pid, $ level) {$ map ['pid '] = $ pid; $ map ['level'] = $ level; $ result = $ this-> where ($ map)-> order ('rank ASC ')-> findall (); if ($ result) {foreach ($ result as $ key => $ value) {$ title = $ value ['Alias']; $ list [$ title] ['id'] = $ value ['id']; $ list [$ title] ['pid '] = $ value ['pid']; $ list [$ title] ['Alias'] = $ value ['Alias']; $ list [$ title] ['title'] = $ value ['title']; $ list [$ title] ['level'] = $ value ['level']; $ list [$ title] ['state'] = $ value ['state']; $ list [$ title] ['rank '] = $ value ['rank']; if ($ value ['level'] <= 3) {$ list [$ title] ['child '] = $ this-> _ MakeSonTree ($ value ['id']) ;}} return $ list ;} function _ MakeSonTree ($ pid) {$ map ['pid '] = $ pid; $ result = $ this-> where ($ map)-> order ('rank ASC ') -> findall (); if ($ result) {foreach ($ result as $ key => $ value) {$ title = $ value ['Alias']; $ list [$ title] ['id'] = $ value ['id']; $ list [$ title] ['pid '] = $ value ['pid']; $ list [$ title] ['Alias'] = $ value ['Alias']; $ list [$ title] ['title'] = $ value ['title']; $ list [$ title] ['level'] = $ value ['level']; $ list [$ title] ['state'] = $ value ['state']; $ list [$ title] ['rank '] = $ value ['rank']; if ($ this-> haschild ($ value ['id']) {// first determine whether a third-level subclass exists. the final array is like $ result ['child '] ['grandchild']; $ list [$ title] ['grandchild '] = $ this-> _ MakeSonTree ($ value ['id']) ;}} return $ list ;} function haschild ($ id) {$ result = D ('learningchannel')-> where ("pid = ". $ id)-> find (); if ($ result) {return true;} else return false ;}
2. bind the volist tag:
$result=D('Learning') ->MakeTree(0,1); //dump($result); $this->assign('list',$result);
3. Template:
selected >{$vo.alias} selected >--{$child.alias} selected >---{$grand.alias}