我正在尝试将php脚本实现为xhtml模板。然而,模板的标记与脚本处理它有点不同。
在模板中我有菜单项和子菜单项。静态导航菜单中的示例代码如下所示:
<li><a href="#">Men TEES</a></li> <-- is a menu item (note the end </li>)
<li><a href="#">WO Tees</a> <-- menu item with submenu items
<ul>
<li><a href="#">Desktop PCs</a></li>
<li><a href="#">Laptop PCs</a></li>
<li><a href="#">TVs</a></li>
<li><a href="#">Printers & Ink</a></li>
<li><a href="#">Electronics & Accesories</a></li>
<li><a href="#">Software</a></li>
<li><a href="#">Sub Navigation</a>
<ul>
php脚本有这段代码来编写菜单和子菜单项:
<?php
//Get all categories
foreach(getCategories($_SESSION['shop']) as $catid=>$cat)
{
echo "<li><a href='?categorie=",$catid,"'>",ucfirst(strtolower($cat)),"</a></li>";
}
//Get all subcategories in categorie
foreach(getSubCategories($catid,$_SESSION['shop']) as $subcatid=>$subcat)
{
echo "<ul><li><a href='?categorie=",$catid,"&subcategorie=",$subcatid,"'>",ucfirst(strtolower($subcat)),"</a></li>";
}
?>
有人可以帮我吗?因为没有子项时它也会写出结束</li>
,如果有一个子项,它就不会写<ul>
,因此标记会混乱。
编辑:这些是php函数:
//Functions to get an array with categories
function getCategories($shopid='')
{
$output=array();
if($shopid!='')
{
//Get categories for this shop
$SQL_get_categorie="SELECT * FROM m4n_category WHERE id IN (SELECT catid FROM m4n_shops_cats WHERE shopid='".$shopid."') order by name";
$SQL_get_categorie_res=mysql_query($SQL_get_categorie);
while($SQL_get_categorie_data=mysql_fetch_array($SQL_get_categorie_res))
{
$id=$SQL_get_categorie_data['id'];
$output[$id]=$SQL_get_categorie_data['name'];
}
}
else
{
//Get all categories
$SQL_get_categorie="SELECT * FROM m4n_category order by name";
$SQL_get_categorie_res=mysql_query($SQL_get_categorie);
while($SQL_get_categorie_data=mysql_fetch_array($SQL_get_categorie_res))
{
$id=$SQL_get_categorie_data['id'];
$output[$id]=$SQL_get_categorie_data['name'];
}
}
return $output;
}
//Functions to get an array with subcategories in a category
function getSubCategories($catid='',$shopid='')
{
$output=array();
if($shopid!='')
{
//Get subcategories for this shop
$SQL_get_categorie="SELECT * FROM m4n_subcategory WHERE category_id='".$catid."' AND id IN (SELECT subcatid FROM m4n_shops_cats WHERE shopid='".$shopid."')order by name";
$SQL_get_categorie_res=mysql_query($SQL_get_categorie);
while($SQL_get_categorie_data=mysql_fetch_array($SQL_get_categorie_res))
{
$id=$SQL_get_categorie_data['id'];
$output[$id]=$SQL_get_categorie_data['name'];
}
}
else
{
//Get all subcategories
$SQL_get_categorie="SELECT * FROM m4n_subcategory WHERE category_id='".$catid."' order by name";
$SQL_get_categorie_res=mysql_query($SQL_get_categorie);
while($SQL_get_categorie_data=mysql_fetch_array($SQL_get_categorie_res))
{
$id=$SQL_get_categorie_data['id'];
$output[$id]=$SQL_get_categorie_data['name'];
}
}
return $output;
}
感谢您的回答,如果一个菜单项包含子菜单项,它不会与</li>
关闭,而是直接从<ul>
开始
答案 0 :(得分:2)
您希望在类别循环中获取子类别,然后可以在正确的位置打开/关闭<ul>
和<li>
。
e.g。
<?php
// Get all categories
foreach(getCategories($_SESSION['shop']) as $catid => $cat)
{
// Notice no </li> at end!
echo "<li><a href='?categorie=",$catid,"'>",ucfirst(strtolower($cat)),"</a>";
// Get all subcategories in category
$subcats = getSubCategories($catid, $_SESSION['shop']);
// If there are some sub categories, then start a sub-list.
if (count($subcats) > 0)
{
echo "<ul>";
foreach($subcats as $subcatid => $subcat)
{
echo "<li><a href='?categorie=",$catid,"&subcategorie=",$subcatid,"'>",ucfirst(strtolower($subcat)),"</a></li>";
}
echo "</ul">;
}
// Now you can close the <li> as the sublist is done.
echo "</li>";
}
?>