Problem: I can’t think of way make a recursion function to my specific situation.
Situation:
Mysql DB
id | root | name |
Where root shows to witch category this is subcategory.
How should HTML look:
<li><a href="#"><p class="Tier0">Datori</p></a>
<ul style="display: block">
<li><a href="#"><p class="Tier1">Cookies</p></a></li>
<li><a href="#"><p class="Tier1">Events</p></a></li>
<li><a href="#"><p class="Tier1">Forms</p></a></li>
<li><a href="#"><p class="Tier1">Games</p></a></li>
<li><a href="#"><p class="Tier1">Images</p></a>
<ul>
<li><a href="#"><p class="Tier2">CSS</p></a></li>
<li><a href="#"><p class="Tier2">JavaScript</p></a></li>
<li><a href="#"><p class="Tier2">JQuery</p></a></li>
</ul>
</li>
<li><a href="#"><p class="Tier1">Navigations</p></a>
<ul>
<li><a href="#"><p class="Tier2">CSS</p></a></li>
<li><a href="#"><p class="Tier2">JavaScript</p></a></li>
<li><a href="#"><p class="Tier2">JQuery</p></a></li>
</ul>
</li>
<li><a href="#"><p class="Tier1">Tabs</p></a></li>
</ul>
</li>
<li><a href="#"><p class="Tier0">Washing Machines</p></a>
What kind of PHP function I would need to print it all out?
How about:
This function requires that you first query your database for the entire list of available categories and assumes that your root categories have a value of null, but the function can be changed to accept -1 or 0 depending on how your current schema works.
You may consider doing the following to prevent any empty UL’s appearing when no children exist for the parent:
However, the best solution, would be to select your data to include a column containing how many child categories each category has.
In which your function would be:
Hope that helps?