gpt4 book ai didi

PHP 将 ul 平均分成 3 列 Joomla 菜单模块

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:47:52 27 4
gpt4 key购买 nike

我目前正在创建一个 joomla 菜单模块,但我遇到了一些问题..

我正在尝试将子菜单项分成 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">';
}

但这只是将它们分成 3 组

这是整个 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>

抱歉了很多代码;我仍在努力学习 PHP 并努力理解 Joomla 的做事方式,这对我来说并不容易。

最佳答案

在循环遍历所有菜单项之前,计算每个菜单项的所有子菜单项。(免责声明:我不了解 Joomla 菜单,但我希望 $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....]
}

关于PHP 将 ul 平均分成 3 列 Joomla 菜单模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19011542/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com