[英]PHP snippet ruins HTML markup
I am trying to implement a php script into a xhtml template. 我正在尝试将php脚本实现为xhtml模板。 The markup of the template however is a little bit different then the script handles it.
然而,模板的标记与脚本处理它有点不同。
In the template i have menu items and submenu items. 在模板中我有菜单项和子菜单项。 A sample code from a static navigation menu looks like this:
静态导航菜单中的示例代码如下所示:
<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>
The php script has this piece of code to write the menu and submenu items: 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>";
}
?>
Can someone help me with this? 有人可以帮我弄这个吗? Cause when there is no subitem it also writes the end
</li>
and if there IS a subitem it doesnt write the <ul>
after it so the markup messes up. 因为当没有子项时它也会写结束
</li>
,如果有一个子项,它就不会在它之后写<ul>
,所以标记会混乱。
EDIT: These are the php functions: 编辑:这些是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;
}
Thanks for the answer, tho if a menu item contains submenu items it doesnt close with a </li>
but starts directly with <ul>
谢谢你的回答,如果一个菜单项包含子菜单项,它没有用
</li>
关闭,但直接从<ul>
You want to get the subcategories within the categories loop, then the <ul>
's and <li>
's can be opened/closed in the correct places. 您希望在类别循环中获取子类别,然后可以在正确的位置打开/关闭
<ul>
和<li>
。
eg 例如
<?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>";
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.