Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6995247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:59:49+00:00 2026-05-27T19:59:49+00:00

I am developing a content management system and I have run into an issue

  • 0

I am developing a content management system and I have run into an issue with child-parent relationships of items in the CMS.

Basically I have a system that can create pages and when a page is created you can select a parent page for sub-navigation. This is all fine and dandy until I try to generate the navigation from the DB.

I’m not sure if some sort of join would be better but I prefer to get all the data in an array and manipulate the array with php.

My array of results from the DB is like this:

Array
(
    [0] => Array
    (
        [id] => 27
        [name] => home
        [link] => home.html
        [parent] => 0
    )

    [1] => Array
    (
        [id] => 30
        [name] => about
        [link] => about.html
        [parent] => 27
    )
)

I need to loop through an array like this that can have any number of navigation and intelligently sort it into its parent child relationships. I was able to do it but only one level deep. It needs to manage children with children with children etc. with an infinite number of layers and output it to HTML unordered nested lists.

<ul>
  <li>
  <a>Nav</a>
     <ul>
        <li>
           <a>Subnav</a>
             <ul>
                 <li>
                    <a>Sub Sub nav</a>
                 </li>
             </ul>
        </li>
     </ul>
  <li>
<ul>

Etc. …

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:59:50+00:00Added an answer on May 27, 2026 at 7:59 pm

    I don’t think you should get into objects. Plus I think it would just be extra work to generate objects and etc. In my opinion you should loop through the array and generate a multidimensional array that represents the navigational hierarchy and then loop the generated array recursively to generate your HTML. I’ve done a sample code for you, it works the way you want it to but you probably want to make some changes.

    functions

    // Generate your multidimensional array from the linear array
    function GenerateNavArray($arr, $parent = 0)
    {
        $pages = Array();
        foreach($arr as $page)
        {
            if($page['parent'] == $parent)
            {
                $page['sub'] = isset($page['sub']) ? $page['sub'] : GenerateNavArray($arr, $page['id']);
                $pages[] = $page;
            }
        }
        return $pages;
    }
    
    // loop the multidimensional array recursively to generate the HTML
    function GenerateNavHTML($nav)
    {
        $html = '';
        foreach($nav as $page)
        {
            $html .= '<ul><li>';
            $html .= '<a href="' . $page['link'] . '">' . $page['name'] . '</a>';
            $html .= GenerateNavHTML($page['sub']);
            $html .= '</li></ul>';
        }
        return $html;
    }
    

    ** sample usage **

    $nav = Array
    (
        Array
        (
            'id' => 27,
            'name' => 'home',
            'link' => 'home.html',
            'parent' => 0
        ),
        Array
        (
            'id' => 30,
            'name' => 'about',
            'link' => 'about.html',
            'parent' => 27
        )
    );
    
    $navarray = GenerateNavArray($nav);
    echo GenerateNavHTML($navarray);
    

    You can probably do both things in one step but I think it’s neater to generate the multidimensional array first. Goodluck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone have any good links for developing custom field and content types that
I am developing a CMS that stores user content in a database table like
I'm developing an add on to a system I have that will allow my
I'm developing a web system that (among other things) will be streaming video content
I am developing a content management system in asp.net where the users of the
I'm developing a class for a content management system. The input content is supplied
I have been developing an application that can publish content like some sort of
I'm developing a browser plugin and server component system that streams content with a
I am developing a custom Content Management System in C# (SQL Server 2005) for
I'm developing a page that will present the user with a content slider, so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.