Yii framework infinitus classification implementation method, yii framework unlimited Classification

Source: Internet
Author: User

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.

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.