I want to display my categories as in the picture, but I got confused with foreach.

enter image description here

As you can see in the picture there is 1 Spoiler is category names 2 A category has a subcategory, then it is without a checkbox, if the subcategory has its own subcategories, they become cheboxes

Please tell me how to correctly implement it!

An example of storing categories in my database:

id | parent_id | title 1 | 0 | Π Π΅ΠΊΠ»Π°ΠΌΠ° 2 | 0 | Автоуслуги 3 | 1 | Настройка контСкстной Ρ€Π΅ΠΊΠ»Π°ΠΌΡ‹ 4 | 3 | SEO оптимизация сайта 5 | 3 | Π Π΅ΠΊΠ»Π°ΠΌΠ° Π² ΡΠΎΡ†ΠΈΠ°Π»ΡŒΠ½Ρ‹Ρ… сСтях 6 | 1 | Услуги ΠΏΡ€ΠΎΠΌΠΎΡƒΡ‚Π΅Ρ€ΠΎΠ² 7 | 6 | ΠœΠ΅Ρ€Ρ‡Π΅Π½Π΄Π°ΠΉΠ·Π΅Ρ€ 9 | 6 | ΠŸΡ€ΠΎΠΌΠΎΡƒΡ‚Π΅Ρ€ Π² супСрмаркСт 10| 6 | БоциологичСскиС опросы 11| 2 | ΠšΡƒΠ·ΠΎΠ²Π½ΠΎΠΉ Ρ€Π΅ΠΌΠΎΠ½Ρ‚ 12| 2 | Π­Π²Π°ΠΊΡƒΠ°Ρ‚ΠΎΡ€ 13| 2 | Зарядка аккумулятора ...|...| .......................... 

How I want it to be

 1menu - menu - menu - menu - menu - menu - menu 2menu - menu - menu - menu - menu - menu - menu 

Thank you in advance!

    2 answers 2

     <?php 

    namespace app \ components; use yii \ base \ Widget; use app \ models \ Category;

    class MenuWidget extends Widget {

     public $tpl; public $data; public $tree; public $menuHtml; public function init(){ parent::init(); if( $this->tpl === null ){ $this->tpl = 'menu'; } $this->tpl .= '.php'; } public function run(){ $this->data = Category::find()->indexBy('id')->asArray()->all(); $this->tree = $this->getTree(); $this->menuHtml = $this->getMenuHtml($this->tree); return $this->menuHtml; } protected function getTree(){ $tree = []; foreach ($this->data as $id=>&$node) { if (!$node['parent_id']) $tree[$id] = &$node; else $this->data[$node['parent_id']]['childs'][$node['id']] = &$node; } return $tree; } protected function getMenuHtml($tree){ $str = ''; foreach ($tree as $category) { $str .= $this->catToTemplate($category); } return $str; } protected function catToTemplate($category){ ob_start(); include __DIR__ . '/menu_tpl/' . $this->tpl; return ob_get_clean(); } 

    }

    • so how do i implement your widget using a picture as an example - Uman
    • so everything is done! the code costs you a tree, you only need to display it. see what the method returns ... - mydls1
    • and how to display it correctly in view ??? - Uman
    • In the init method it is checked whether there is a user parameter. if not, use menu view - the name of the file is recorded in the tpl name. in the catToTemplate method, this template is connected, and in the template you can use the parameter passed to the method - mydls1
    • I certainly apologize for my stupidity, but you can take an example - Uman

    you need to know the essence of recursion and building a tree for it For yii there is an example http://yiiframework.ru/forum/viewtopic.php?t=14520 Briefly the essence is shown in How to correctly implement the construction of a tree through recursion?

    • Damn, I can not understand in the model I have a method public static function getCategory () {$ category = Category :: find () -> asArray () -> all (); } can you help me ??? - Uman