我想遍历出一个顶级分类下的所有子分类,尝试了很多方法都没成功,有点转不过弯来了,希望老师指点一下。
//方法一 //在CommonController里面只能得到最后一个顶级栏目下的分类内容? $menu = Category::getMenu(); $this->view->params['menu'] = $menu; $fllb = []; foreach ($menu as $key){ $fllb = Category::find()->where('parentid = :pid', [":pid" => $key['cateid']])->asArray()->all(); } $this->view->params['fllb'] = $fllb;
//方法二 //找出了全部的子分类 <?php foreach ($this->params['menu'] as $top): ?> <?php foreach($top['children'] as $child): ?> <?php if ($child['parentid'] == $top['cateid']): ?> <li><a href="<?php echo yii\helpers\Url::to(['product/index', 'cateid' => $child['cateid']]); ?>"> <?php echo $child['title']; ?> </a></li> <?php endif; ?> <?php endforeach; ?> <?php endforeach; ?>