1

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:

  • webpageId
  • title
  • parentWebpageId

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.

1
  • Nothing I'm proud of, I fear. Commented Nov 15, 2013 at 12:19

1 Answer 1

1

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:

enter image description here

The article in question has a lot of example queries that you can look at and use.

Sign up to request clarification or add additional context in comments.

2 Comments

@AgRizzo Whoops! Copied and pasted the wrong title. Thanks for pointing that out.
Although I haven't been able to solve my problem, the link you provided was an interesting read. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.