The idea is to save all webpage data (title, description, open graph tags, etc...) into a database table. Of course, some of the pages are supposed to be children of other pages. For example, there should be "marketing" and "photography" under "services".
For simplification purposes, let's say that the table looks like this:
I need to find a way to loop through all this data, and print an HTML list using PHP:
<ul>
<li>home</li>
<li>
services
<ul>
<li>marketing</ul>
<li>photography</ul>
</ul>
</li>
</ul>
I'm trying to build a system which should be reusable, and there is no way to know how deep the recursion might go, so I need a way to always be sure I checked everything.
Using foreach loops inside one another doesn't seem like the best solution.
Thanks in advance.
Basically, you're dealing with hierarchical data. Recursion (or multiple loops inside multiple loops) isn't the solution because you don't want to be running an endless amount of SQL queries, just to display a navigation list. If you look at this article, you'll see that there are two methods / structures that you can use. The Adjacency List Model
and the Nested Set Model
.
The Nested Set Model
looks like this:
The article in question has a lot of example queries that you can look at and use.