A
A
Anton2016-12-13 16:13:57
Kohana
Anton, 2016-12-13 16:13:57

4 level menu, I can't figure out how to display level 4?

Hello, I did level 3, I tried to do 4, I could not. I did it according to examples in the internet, there was max 3.
In the php code, you can see that 5 levels have been drawn.
But in html, the problem. there you need to add the code, as I understand it, so that the 4th level is displayed.
HTML

<ul class="sky-mega-menul sky-mega-menu-pos-left">
<?php foreach ($categories as $category):
if ($category['show_cat'] == '1'):
if (!isset($category['children']))
{
?>
<li><a href="/category/<?php echo $category['alias'] ?>/" title="<?= $category['name'] ?>"><?= $category['name'] ?></a></li>
<?
}
else
{
?>

<li aria-haspopup="true">
<?php $showChild = false; foreach ($category["children"] as $childfirst):
if ($childfirst['show_cat'] == '1')
{
$showChild = true;
}
break;
endforeach;
if ($showChild)
{
?>
<ul>
<?php foreach ($category["children"] as $childfirst):
if ($childfirst['show_cat'] == '1')
{
if (!isset($childfirst['children']))
{
?>
<li><a href="/category/<?= $childfirst['alias'] ?>/" title="<?= $childfirst['name'] ?>"><?= $childfirst['name'] ?></a></li>
<?
}
else
{
$showChilds = false;
foreach ($childfirst["children"] as $childsecond):
if ($childsecond['show_cat'] == '1')
{
$showChilds = true;
}
break;
endforeach;
if ($showChilds)
{
?>
<li class="parent">
<a href="/category/<?= $childfirst['alias'] ?>/" title="<?= $childfirst['name'] ?>"><?= $childfirst['name'] ?></a>
<? ?>
<div class="grid-container3">
<ul>
<?php foreach ($childfirst["children"] as $childsecond): ?>
<li><a href="/category/<?= $childsecond['alias'] ?>/" title="<?= $childsecond['name'] ?>"><?= $childsecond['name'] ?></a></li>
<? endforeach; ?>
</ul>
</div>
</li>
<?
}
else
{
?>
<li><a href="/category/<?= $childfirst['alias'] ?>/" title="<?= $childfirst['name'] ?>"><?= $childfirst['name'] ?></a></li>
<?
}
}
}
endforeach;
?>
</ul>
</li>

<?
}
?>
<?
}
endif;
endforeach;
?>
</ul>

PHP
$cats = ORM::factory('category')
                   ->order_by('position', 'ASC')
                   ->find_all();
$cilddel = array();
        foreach ($cats as $cat)
        {
            $data[$cat->id]['name'] = $cat->name;
            $data[$cat->id]['id'] = $cat->id;
            $data[$cat->id]['alias'] = $cat->alias;
            $data[$cat->id]['cattext'] = $cat->cattext;
            $data[$cat->id]['show_cat'] = $cat->show_cat;
            $data[$cat->id]['position'] = $cat->position;
            
            $ch1 = ORM::factory('category')
                      ->where('parent_id', '=', $cat->id)
                      ->order_by('position', 'ASC')
                      ->find_all();
            foreach ($ch1 as $c1)
            {
                $cilddel[$c1->id] = $c1->id;
                $data[$cat->id]['children'][$c1->id]['name'] = $c1->name;
                $data[$cat->id]['children'][$c1->id]['alias'] = $c1->alias;
                $data[$cat->id]['children'][$c1->id]['id'] = $c1->id;
                $data[$cat->id]['children'][$c1->id]['cattext'] = $c1->cattext;
                $data[$cat->id]['children'][$c1->id]['show_cat'] = $c1->show_cat;
                $data[$cat->id]['children'][$c1->id]['position'] = $c1->position;
                
                $ch2 = ORM::factory('category')
                          ->where('parent_id', '=', $c1->id)
                          ->order_by('position', 'ASC')
                          ->find_all();
                foreach ($ch2 as $c2)
                {
                    $data[$cat->id]['children'][$c1->id]['children'][$c2->id]['name'] = $c2->name;
                    $data[$cat->id]['children'][$c1->id]['children'][$c2->id]['alias'] = $c2->alias;
                    $data[$cat->id]['children'][$c1->id]['children'][$c2->id]['id'] = $c2->id;
                    $data[$cat->id]['children'][$c1->id]['children'][$c2->id]['cattext'] = $c2->cattext;
                    $data[$cat->id]['children'][$c1->id]['children'][$c2->id]['show_cat'] = $c2->show_cat;
                    $data[$cat->id]['children'][$c1->id]['children'][$c2->id]['position'] = $c2->position;
                    
                    $ch3 = ORM::factory('category')
                              ->where('parent_id', '=', $c2->id)
                              ->order_by('position', 'ASC')
                              ->find_all();
                    foreach ($ch3 as $c3)
                    {
                        $data[$cat->id]['children'][$c1->id]['children'][$c2->id]['children'][$c3->id]['name'] =
                            $c3->name;
                        $data[$cat->id]['children'][$c1->id]['children'][$c2->id]['children'][$c3->id]['alias'] =
                            $c3->alias;
                        $data[$cat->id]['children'][$c1->id]['children'][$c2->id]['children'][$c3->id]['id'] = $c3->id;
                        $data[$cat->id]['children'][$c1->id]['children'][$c2->id]['children'][$c3->id]['cattext'] =
                            $c3->cattext;
                        $data[$cat->id]['children'][$c1->id]['children'][$c2->id]['children'][$c3->id]['show_cat'] =
                            $c3->show_cat;
                        $data[$cat->id]['children'][$c1->id]['children'][$c2->id]['children'][$c3->id]['position'] =
                            $c3->position;
                        
                        $ch4 = ORM::factory('category')
                                  ->where('parent_id', '=', $c3->id)
                                  ->order_by('position', 'ASC')
                                  ->find_all();
                        foreach ($ch4 as $c4)
                        {
                            $data[$cat->id]['children'][$c1->id]['children'][$c2->id]['children'][$c3->id]['children'][$c4->id]['name'] =
                                $c4->name;
                            $data[$cat->id]['children'][$c1->id]['children'][$c2->id]['children'][$c3->id]['children'][$c4->id]['alias'] =
                                $c4->alias;
                            $data[$cat->id]['children'][$c1->id]['children'][$c2->id]['children'][$c3->id]['children'][$c4->id]['id'] =
                                $c4->id;
                            $data[$cat->id]['children'][$c1->id]['children'][$c2->id]['children'][$c3->id]['children'][$c4->id]['cattext'] =
                                $c4->cattext;
                            $data[$cat->id]['children'][$c1->id]['children'][$c2->id]['children'][$c3->id]['children'][$c4->id]['show_cat'] =
                                $c4->show_cat;
                            $data[$cat->id]['children'][$c1->id]['children'][$c2->id]['children'][$c3->id]['children'][$c4->id]['position'] =
                                $c4->position;
                            
                            $ch5 = ORM::factory('category')
                                      ->where('parent_id', '=', $c4->id)
                                      ->order_by('position', 'ASC')
                                      ->find_all();
                            foreach ($ch5 as $c5)
                            {
                                $data[$cat->id]
                                ['children'][$c1->id]
                                ['children'][$c2->id]
                                ['children'][$c3->id]
                                ['children'][$c4->id]
                                ['children'][$c5->id]
                                ['name'] = $c5->name;
                                
                                $data[$cat->id]
                                ['children'][$c1->id]
                                ['children'][$c2->id]
                                ['children'][$c3->id]
                                ['children'][$c4->id]
                                ['children'][$c5->id]
                                ['alias'] = $c5->alias;
                                
                                $data[$cat->id]
                                ['children'][$c1->id]
                                ['children'][$c2->id]
                                ['children'][$c3->id]
                                ['children'][$c4->id]
                                ['children'][$c5->id]
                                ['id'] = $c5->id;
                                
                                $data[$cat->id]
                                ['children'][$c1->id]
                                ['children'][$c2->id]
                                ['children'][$c3->id]
                                ['children'][$c4->id]
                                ['children'][$c5->id]
                                ['cattext'] = $c5->cattext;
                                
                                $data[$cat->id]
                                ['children'][$c1->id]
                                ['children'][$c2->id]
                                ['children'][$c3->id]
                                ['children'][$c4->id]
                                ['children'][$c5->id]
                                ['show_cat'] = $c5->show_cat;
                                
                                $data[$cat->id]
                                ['children'][$c1->id]
                                ['children'][$c2->id]
                                ['children'][$c3->id]
                                ['children'][$c4->id]
                                ['children'][$c5->id]
                                ['position'] = $c5->position;
}
}
}
}
}

}
        
        foreach ($cilddel as $key => $item)
        {
            unset($data[$key]);
        }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Shamanov, 2016-12-13
@SilenceOfWinter

read about recursions, in general nested sets table structure is used to build multi-level menus, for kohana there is an mptt module.

A
Anton, 2016-12-13
@karminski

I would answer you: if you have 4! menu level, you need to rethink the structure of the application.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question