I have an array filled with "Category" objects, each one containing an id and a parent_id value. They're accessed like this:
$category->get("id"); //gets category id
$category->get("parent_id"); gets parent category id
I want to make a HTML UL list like this:
<ul>
<li>Category</li>
<li>Category
<ul>
<li>Child Category</li>
<li>Child Category</li>
<li>Child Category
<ul>
<li>Child Category</li>
<li>Child Category</li>
<li>Child Category</li>
</ul>
</li>
</ul>
</li>
<li>Category</li>
<li>Category</li>
<li>Category</li>
<li>Category</li>
</ul>
What sort of loop must be done to generate this?
The MPTT tree logic might be useful
http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/
without knowing what library you are using, it will be more of a pseudo code than working code but you should get the idea of how to use recursion to get the tree
first retrieve the main category (you could set that for example as category 0 is the top category. then loop through all items and fetch children. If node has children, call itself recursively
showCategory($rootcategory)
function showCategory($category) {
$children=fetchChildren();
if($children) //if category has children
{
echo('<ul>');
foreach($children as $child) {
showCategory($child);
}
echo('</ul>');
}
else {
echo('<li>' . $child['title'] . '</li>');
}
}