我正在尝试使用 PHP 创建一个 Mega Menu,但在正确输出结构时遇到问题。我已经对 Mega Menu 进行了硬编码来测试所有内容,它工作正常,但显然我需要 PHP 来为我创建它。
我有一个 Mega Menu 硬编码的示例,因此每个人都可以看到我正在尝试创建的内容:
http://www.libertyeaglearms.com/dev
或者这里的代码:
期望的输出:
<div id="wrapper">
<ul class="mega-menu">
<li class="mega-menu-drop">
<a href="#">Firearms</a>
<div class="mega-menu-content">
<div class="column">
<h4>Rifles</h4>
<ul>
<li><a href="#">One</a></li>
<li><a href="#">Two</a></li>
<li><a href="#">Three</a></li>
<li><a href="#">Four</a></li>
<li><a href="#">Five</a></li>
</ul>
</div>
<div class="column">
<h4>Handguns</h4>
<ul>
<li><a href="#">One</a></li>
<li><a href="#">Two</a></li>
<li><a href="#">Three</a></li>
<li><a href="#">Four</a></li>
<li><a href="#">Five</a></li>
</ul>
</div>
<div class="column">
<h4>Shotguns</h4>
<ul>
<li><a href="#">One</a></li>
<li><a href="#">Two</a></li>
<li><a href="#">Three</a></li>
<li><a href="#">Four</a></li>
<li><a href="#">Five</a></li>
</ul>
</div>
</div>
</li>
<li class="mega-menu-drop">
<a href="#">Archery</a>
<div class="mega-menu-content">
<div class="column">
<h4>Bows</h4>
<ul>
<li><a href="#">One</a></li>
<li><a href="#">Two</a></li>
<li><a href="#">Three</a></li>
<li><a href="#">Four</a></li>
<li><a href="#">Five</a></li>
</ul>
</div>
<div class="column">
<h4>Arrows</h4>
<ul>
<li><a href="#">One</a></li>
<li><a href="#">Two</a></li>
<li><a href="#">Three</a></li>
<li><a href="#">Four</a></li>
<li><a href="#">Five</a></li>
</ul>
</div>
</div>
</li>
</ul>
</div>
当前输出:(非常混乱。被警告,大声笑)
<div id="wrapper">
<ul class="mega-menu">
<li class="mega-menu-drop">
<a href="#">archery</a>
<div class="mega-menu-content">
<div class="column">
<h4>compound</h4>
<ul>
<h4>bows</h4>
<ul>
</div>
</li>
<li class="mega-menu-drop">
<a href="#">firearms</a>
<div class="mega-menu-content">
<div class="column">
<h4>rifle ammunition</h4>
<ul>
<h4>ammunition</h4>
<ul>
</div>
</li>
</ul>
</div>
这是我的PHP:
$json = json_decode($category->buildDepartments(NULL),true);
function buildDepartments($array,$parent)
{
$html = '';
foreach($array as $category)
{
if($category['parent'] == $parent)
{
if($category['parent'] == NULL)
{
$html .= '<li class="mega-menu-drop">' . "\n\t\t\t\t";
$html .= '<a href="#">'.$category['category_name'].'</a>' . "\n\t\t\t\t";
$html .= '<div class="mega-menu-content">' . "\n\t\t\t\t\t";
$html .= '<div class="column">' . "\n\t\t\t\t\t\t";
$html .= buildDepartments($array,$category['category_id']);
$html .= '</div>' . "\n\t\t\t";
$html .= '</li>' . "\n\t\t\t";
}
else
{
$html .= buildDepartments($array,$category['category_id']);
$html .= '<h4>'.$category['category_name'].'</h4>' . "\n\t\t\t\t\t\t";
$html .= '<ul>' . "\n\t\t\t\t";
}
}
}
return $html;
}
print(buildDepartments($json,NULL));
这是我的数据库:
赏金后编辑
根据 icktoofay 的建议,我似乎无法弄清楚foreach()
循环。我遇到的问题是当我应该看到类别和子类别时,我在大型菜单中获得了部门名称。我认为问题是我需要使用特定的父 ID 执行循环才能获取所有孩子,但我不确定这是否是问题所在。这是代码:
<div id="wrapper">
<ul class="mega-menu">
<?php $json = json_decode($category->buildDepartments(NULL)); ?>
<?php foreach($json as $category): ?>
<li class="mega-menu-drop">
<a href="#"><?php if($category->parent === NULL){echo $category->category_name;} ?></a>
<div class="mega-menu-content">
<?php foreach($category as $subcategory): ?>
<div class="column">
<?php if($category->category_id === $category->parent){echo '<h4>' . $category->category_name . '</h4>';}?>
<ul>
<?php foreach($category as $subcategory)
{
echo '<li>' . $category->category_name . '</li>';
}
?>
</ul>
</div>
<?php endforeach; ?>
</div>
</li>
<?php endforeach; ?>
</ul>
</div>