我目前正在創建一個Joomla菜單模塊,但香港專業教育學院遇到的一些問題..PHP分裂UL均勻成3列的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的做事方式,但這對我來說並不是那麼容易。
我沒潛水過深地進入代碼,但是如果我正確地理解了設計,爲什麼要在打開和關閉這個'ul'應該包含的'li'之間放置一段關閉'ul'的代碼? – MasterAM
,因爲我不知道我在做什麼?..我解決了大部分問題,現在只是將它們分成3列......不適合更新我的問題..感謝您的回覆 –
由於您已經有了一個分層列表,在HTML中不可能(就像你在一般意義上做的那樣)創建多個列。我將使用'RecursiveIterator',因爲它可以跟蹤級別,因爲您需要在一列結束時關閉整個UL/LI層次結構,並在下一列開始時重新打開它。解釋起來並不容易,編寫代碼也很容易,一些相關代碼可能與以下鏈接有關:[檢查n深度樹中的值?](http://stackoverflow.com/a/12758996/367456) – hakre