Yii framework infinitus classification implementation method, yii framework unlimited Classification
The yii framework is used to create an infinitus classification. The main array conversion is to paste other people's code, but it is easy to write it out.
YesRecursion, Not the path
Controller:
Protected function subtree ($ arr, $ id = 0, $ lev= 1) {$ subs = array (); // descendant array foreach ($ arr as $ v) {if ($ v ['parent _ id'] =$ id) {$ v ['lev'] = $ lev; $ subs [] = $ v; // For example, find array ('id' => 1, 'name' => 'anhui ', 'parent' => 0), $ subs = array_merge ($ subs, $ this-> subtree ($ arr, $ v ['cat _ id'], $ lev+ 1) ;}return $ subs;} public function actionCreate () {$ model = new EcsCategory (); $ query = new \ yii \ db \ Query (); $ query-> select ('*') -> from ('ecs _ category '); $ command = $ query-> createCommand (); $ res = $ command-> queryAll (); $ tree = $ this-> subtree ($ res, 0, 1); foreach ($ tree as $ k => $ v) {$ tree [$ k] ['new _ cat_name '] = str_repeat (' -- ', $ v ['lev']). $ v ['cat _ name']. str_repeat ('--', $ v ['lev']); // str_repeat-repeat a string} $ arr = array ('new _ cat_name '=> 'top-level category ', 'cat _ id' => 0); array_unshift ($ tree, $ arr); if ($ model-> load (Yii :: $ app-> request-> post () & $ model-> save () {return $ this-> redirect (['view ', 'id' => $ model-> cat_id]);} else {return $ this-> render ('create', ['model' => $ model, 'data' => $ tree,]) ;}}
View:
Use \ yii \ helpers \ ArrayHelper; <? = $ Form-> field ($ model, 'parent _ id')-> dropDownList (ArrayHelper: map ($ data, 'cat _ id ', 'New _ cat_name '), ['propt' => 'select a parent category'])?>
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.