简体   繁体   English

PHP将ul分成均匀分为3列Joomla Menu Module

[英]PHP split ul evenly into 3 columns Joomla Menu Module

I am currently creating a joomla menu module, but Ive come across some issues.. 我目前正在创建一个joomla菜单模块,但我遇到了一些问题..

i am trying to split the sub menu items into 3 columns, currently i am using this: 我试图将子菜单项分成3列,目前我正在使用这个:

$counter = 0;

if($item->level == 2):
    $counter += count($item);
endif;

if($item->level == 1):
    $counter = 0;
endif;

if($counter%3 == 0 && $item->level == 2){
        echo '</ul><ul class="col-lg-3">';
    }

but this just groups them into 3's 但这只是将它们分成3个

Here's the entire default.php: 这是整个default.php:

<?php

// No direct access
defined('_JEXEC') or die;

// Note. It is important to remove spaces between elements.
$counter = 0;

?>

<ul class="nav navbar-nav <?php echo $class_sfx; ?>  nav-mega"<?php
$tag = '';
if ($params->get('tag_id') != null)
{
    $tag = $params->get('tag_id') . '';
    echo ' id="' . $tag . '"';
}
?>>

<?php
    foreach ($list as $i => &$item) {

        if($item->level == 2):
            $counter += count($item);
        endif;

        if($item->level == 1):
            $counter = 0;
        endif;

        $class = 'item-' . $item->id;
        if ($item->id == $active_id) {
            $class .= ' current';
        }

        if (in_array($item->id, $path)){
            $class .= ' active';
        }elseif ($item->type == 'alias'){
            $aliasToId = $item->params->get('aliasoptions');
            if (count($path) > 0 && $aliasToId == $path[count($path) - 1]) {
                $class .= ' active';
            }elseif (in_array($aliasToId, $path)){
                $class .= ' alias-parent-active';
            }
        }

        if ($item->deeper){
            $class .= ' deeper dropdown';
        }

        if ($item->parent){
            $class .= ' parent';
        }

        if (!empty($class)){
            $class = ' class="' . trim($class) . '"';
        }

        echo '<li' . $class . '>';

        // Render the menu item.
        switch ($item->type){
            case 'separator':
            case 'url':
            case 'component':
                require JModuleHelper::getLayoutPath('mod_blogmenu', 'default_' . $item->type);
            break;

            default:
                require JModuleHelper::getLayoutPath('mod_blogmenu', 'default_url');
            break;
        }

        // The next item is deeper.
        if($counter%3 == 0 && $item->level == 2){
            echo '</ul><ul class="col-lg-3">';
        }

        if ($item->deeper){
            echo '<div class="dropdown-menu mega-dropdown">';
            echo '<div class="mega-image col-lg-3 thumbnail visible-md visible-lg"><img src="'.$item->menu_image.'" /></div>';
            echo '<ul class="col-lg-3">';
        }
                // The next item is shallower.
        elseif ($item->shallower){

            echo str_repeat('</ul><div class="mega-caption"></div></div>', $item->level_diff);
        }
                // The next item is on the same level.
        else {
            //echo '</li>';
        }
    }

?>
</ul>

Sorry a lot of code; 对不起很多代码; I am still trying to learn PHP and trying to understand Joomla's way of doing things, it isn't all too easy for me. 我仍然在努力学习PHP并试图了解Joomla的做事方式,这对我来说并不容易。

Before you loop through all menuitems, count all the submenuitems for each menuitem. 在循环浏览所有菜单项之前,请计算每个菜单项的所有子菜单。 (Disclaimer: I have no knowledge of Joomla Menus, but I'm hoping $item->parent->id references to id of the parent item.) (免责声明:我不了解Joomla Menus,但我希望$ item-> parent-> id引用父项的id。)

$submenuItemsTotals = array();
foreach ($list as $i => &$item) { 
   if ($item->level == 2) {
       if (!isset($submenuItemsTotal[$item->parent->id])) {
          $submenuItemsTotal[$item->parent->id] = 1;
       } else {
          $submenuItemsTotal[$item->parent->id]++;
       }
   }
}

$itemsPerColumn = array();
foreach ($submenuItemsTotals as $parentId => $submenuItemsTotal) {
   $itemsPerColumn[$parentId] = ceil($submenuItemsTotal / 3); 
}


// Here comes your existing code with a small change

foreach ($list as $i => &$item) { 
    [your code....]

    // The next item is deeper.
    if($item->level == 2 && ($counter % $itemsPerColumn[$item->parent->id]) == 0){
        echo '</ul><ul class="col-lg-3">';
    }

    [your code....]
}

Can you put it into CSS columns? 你能把它放入CSS专栏吗?

<!DOCTYPE html>
<html>
<head>

<style>
div.columns {
column-width: auto;
-moz-column-width: auto;
-webkit-column-width: auto;
column-count: 3;
-moz-column-count: 3;
-webkit-column-count: 3;
column-gap: 1em;
-moz-column-gap: 1em;
-webkit-column-gap: 1em;
}

.nosplit {
display: inline-block;
}
</style>

</head>

<body>

<div class="columns">
<ul>
<li>item01</li>
<li>item02</li>
<li>item03</li>
<li>item04</li>
<li class="nosplit">item05
    <ul>
    <li>item05a</li>
    <li>item05b</li>
    </ul>
    </li>
<li>item06</li>
<li>item07</li>
<li class="nosplit">item08
    <ul>
    <li>item08a</li>
    <li>item08b</li>
    <li>item08c</li>
    </ul>
    </li>
<li>item09</li>
<li>item10</li>
</ul>

</body>

</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM