【发布时间】:2018-03-26 19:20:11
【问题描述】:
使用 PHP,我需要创建一个三层深度的导航栏。我有这样的数据:
我需要这样组织:
Napkins
--Colored Napkins
----American Tradition
--White Beverage Napkins
----American Digital
----American Tradition
----American Hi-Speed
--White Luncheon Napkins
----American Digital
----American Tradition
----American Hi-Speed
--White Dinner Napkins
...(etc)...
Plates
--Eco-Plates
----American Tradition
--Plastic Trays
...(etc)...
只是使用了一个相对简单的<ul> 结构与<li> 和<a>。显然需要一个循环来遍历数据行。而且我知道我需要做很多“测试”来查看“当前”类别/子类别/方法是否与循环的上一次迭代中的匹配。
但是当只有 一个 打印方法时,我无法让 <ul> and <li> 标记在正确的位置关闭并且不显示打印方法层对于给定的类别/子类别。或者当有no打印方法时(有时可以是null,比如塑料器皿)。我该如何设置?
编辑
我目前的代码,几乎可以运行,但是非常笨重($items 是数据库中的数据):
function addMethodMenuItem($result, $short_cat_name, $short_subcat_name, $short_method_name, $this_method_name)
{
$result .= "<li>";
$result .= "<a";
$result .= " href=\"product.php?category={$short_cat_name}&subcategory={$short_subcat_name}&printMethod={$short_method_name}\"";
$result .= " target=\"_self\"";
$result .= " title=\"{$this_method_name}\">";
$result .= $this_method_name;
$result .= "</a>";
$result .= "</li>";
return $result;
}
function addSubMenuItem($item, $previous_cat_name, $previous_subcat_name, $result, $newCat)
{
$this_cat_name = $item["category_name"];
$this_subcat_name = $item["subcategory_name"];
$this_method_name = $item["method_name"];
$short_cat_name = $item["category_short"];
$short_subcat_name = $item["subcategory_short"];
$short_method_name = $item["method_short"];
if ($this_subcat_name != $previous_subcat_name || $this_cat_name != $previous_cat_name) { // We have to create a new "subcategory" menu item
if ($previous_subcat_name != null && !$newCat) { // if this isn't the first subcategory menu item of the category.
$result .= "</ul></li>";
}
$result .= "<li>";
$result .= "<a class=\"ajxsub\"";
$result .= " href=\"#\">";
$result .= $this_subcat_name;
$result .= "</a>";
$result .= "<ul>";
}
$result = addMethodMenuItem($result, $short_cat_name, $short_subcat_name, $short_method_name, $this_method_name);
return $result;
}
function printMenu(array $items, $previous_cat_name, $result)
{
// $result .= "<ul>";
$previous_subcat_name = null;
foreach ($items as $item) {
$newCat = false;
$this_cat_name = $item["category_name"];
$this_subcat_name = $item["subcategory_name"];
if ($this_cat_name != $previous_cat_name) {
if ($previous_cat_name != null) { // if this isn't the very first top-level menu item.
$result .= "</ul></li>";
$result .= "</ul></li>";
$newCat = true;
}
$result .= "<li>";
$result .= "<a class=\"ajxsub\"";
$result .= " href=\"#\">";
$result .= $this_cat_name;
$result .= "</a>";
$result .= "<ul>";
}
$result = addSubMenuItem($item, $previous_cat_name, $previous_subcat_name, $result, $newCat);
$previous_subcat_name = $this_subcat_name;
$previous_cat_name = $this_cat_name;
}
$result .= "</ul></li>";
// $result .= "</ul>";
return $result;
}
【问题讨论】:
-
你有tried anything so far吗?请更新您的问题以显示您已经尝试过的内容,展示您在minimal, complete, and verifiable example 中面临的特定问题。
-
对不起;我忘记在其中添加我现有的代码。
标签: php html-lists navbar hierarchy