I have been trying to build a child/parent navigation for a little while now using PHP (I'm from a .net background) and I can't get close to the desired results. I am loading my data from an xml file using simplexml successfully but I am trying to workout how I can map that into arrays or variables so I can write it out.
<Categories>
<Category>
<ID>1</ID>
<Title>Days</Title>
<Description />
<ParentID />
<Meta />
</Category>
<Category>
<ID>2</ID>
<Title>Monday</Title>
<Description />
<ParentID>1</ParentID>
<Meta />
</Category>
<Category>
<ID>3</ID>
<Title>Tuesday</Title>
<Description />
<ParentID>1</ParentID>
<Meta />
</Category>
<Category>
<ID>4</ID>
<Title>Wednesday</Title>
<Description />
<ParentID>1</ParentID>
<Meta />
</Category>
<Category>
<ID>5</ID>
<Title>Thursday</Title>
<Description />
<ParentID>1</ParentID>
<Meta />
</Category>
<Category>
<ID>6</ID>
<Title>Friday</Title>
<Description />
<ParentID>1</ParentID>
<Meta />
</Category>
<Category>
<ID>7</ID>
<Title>Saturday</Title>
<Description />
<ParentID/>
<Meta />
</Category>
<Category>
<ID>8</ID>
<Title>Sunday</Title>
<Description />
<ParentID/>
<Meta />
</Category>
</Categories>
foreach($categories as $category) {
if ($category->ParentID != "")
{
echo "<li><a href=index.php?Cat=$category->ID>$category->Title</a></li>";
echo "<ul>";
foreach($categories as $subcategory) {
if ($subcategory->ParentID == $category->ID)
{
echo "<li><a href=index.php?Cat=$subcategory->ID>$subcategory->Title</a></li>";
}
}
echo "</ul>";
}
else
{
echo "<li><a href=index.php?Cat=$category->ID>$category->Title</a></li>";
}
}
echo "</ul>";
So my desired output would be something like this:
<ul id="p7menubar">
<li><a class="trigger" href="#">Days</a>
<ul>
<li><a href="#">Monday</a></li>
<li><a href="#">Tuesday</a></li>
<li><a href="#">Wednesday</a></li>
<li><a href="#">Thursday</a></li>
<li><a href="#">Friday</a></li>
</ul>
</li>
<li><a href="index.htm">Saturday</a></li>
<li><a href="index.htm">Sunday</a></li>
</ul>