menu
- menu
- menu
- menu
- menu
- menu
- menu
menu
- menu
- menu
- menu
- menu
- menu
- menu
$menu = Array( // Предполагалось, что он SQL SELECT
Array('id'=>1,'title'=>'Menu 1', 'parent_id'=>null),
Array('id'=>2,'title'=>'Sub 1.1', 'parent_id'=>1),
Array('id'=>3,'title'=>'Sub 1.2', 'parent_id'=>1),
Array('id'=>4,'title'=>'Sub 1.3', 'parent_id'=>1),
Array('id'=>5,'title'=>'Menu 2', 'parent_id'=>null),
Array('id'=>6,'title'=>'Sub 2.1', 'parent_id'=>5),
Array('id'=>7,'title'=>'Sub Sub 2.1.1', 'parent_id'=>6),
Array('id'=>8,'title'=>'Sub 2.2', 'parent_id'=>5),
Array('id'=>9,'title'=>'Menu 3', 'parent_id'=>null),
);
function has_children($rows,$id) {
foreach ($rows as $row) {
if ($row['parent_id'] == $id)
return true;
}
return false;
}
function build_menu($rows,$parent=0)
{
$result = "<ul>";
foreach ($rows as $row)
{
if ($row['parent_id'] == $parent){
$result.= "<li>{$row[title]}";
if (has_children($rows,$row['id']))
$result.= build_menu($rows,$row['id']);
$result.= "</li>";
}
}
$result.= "</ul>";
return $result;
}
echo build_menu($menu);
<ul>
<li>Menu 1<ul>
<li>Sub 1.1</li>
<li>Sub 1.2</li>
<li>Sub 1.3</li>
</ul></li>
<li>Menu 2<ul>
<li>Sub 2.1<ul>
<li>Sub Sub 2.1.1</li>
</ul></li>
<li>Sub 2.2</li>
</ul></li>
<li>Menu 3</li>
</ul>
<?php
namespace app\components;
use Yii;
use yii\base\Widget;
use app\models\Category;
class CategoryWidget extends Widget
{
public $tpl;
public $model;
public $data;
public $tree;
public $menuHtml;
public function init()
{
parent::init();
if ($this->tpl === null) {
$this->tpl = 'category';
}
$this->tpl .= '.php';
}
public function run()
{
// get cache
// if ($this->tpl == 'category.php') {
// $menu = Yii::$app->cache->get('category');
// if ($menu) return $menu;
// }
$this->data = Category::find()
->indexBy('id')
->orderBy('sortOrder')
->asArray()
->all();
$this->tree = $this->getTree();
$this->menuHtml = $this->getMenuHtml($this->tree);
// set cache
// if ($this->tpl == 'category.php') {
// Yii::$app->cache->set('category', $this->menuHtml, 60);
// }
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']]['children'][$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__ . '/category_tpl/' . $this->tpl;
return ob_get_clean();
}
}
<?= $category['title'] ?>
<?php if (isset($category['children'])): ?><br/>
<?= $this->getMenuHtml($category['children']) ?>
<?php endif; ?>
<?= CategoryWidget::widget(['tpl' => 'category']) ?>