我正在尝试将php脚本实现为xhtml模板。然而,模板的标记与脚本处理它有点不同。
在模板中我有菜单项和子菜单项。静态导航菜单中的示例代码如下所示:
Men TEES ) WO Tees- Desktop PCs
- Laptop PCs
- TVs
- Printers & Ink
- Electronics & Accesories
- Software
- Sub Navigation
php脚本有这段代码来编写菜单和子菜单项:
//Get all categories
foreach(getCategories($_SESSION['shop']) as $catid=>$cat)
{
echo "
- ",ucfirst(strtolower($cat)),"";
}
//Get all subcategories in categorie
foreach(getSubCategories($catid,$_SESSION['shop']) as $subcatid=>$subcat)
{
echo "
}
?>
有人可以帮我吗?因为没有子项时它也会写出结束
,如果有一个子项,它就不会写- ,因此标记会混乱。
编辑:这些是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;
}
感谢您的回答,如果一个菜单项包含子菜单项,它不会与
关闭,而是直接从- 开始