Reputation: 11
I am trying to implement a php script into a xhtml template. 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
//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.
EDIT: These are the php functions:
//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>
Upvotes: 0
Views: 119
Reputation: 26574
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.
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>";
}
?>
Upvotes: 2