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 1008317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:45:13+00:00 2026-05-16T08:45:13+00:00

Here’s the situation: I have a menu that needs to be created dynamically from

  • 0

Here’s the situation:
I have a menu that needs to be created dynamically from the database.
The menu hierarchy is determined by a ‘parent’ column in the table (each entry has one parent or NULL if it is only a parent)

The problem is that I can’t think of how I would dynamically do this, considering I need proper <ul><li><ul><li> structure for my drop-down menu.
This requires that I have my ‘foreach’ of child pages, within the foreach of parent pages?
If that makes sense, is there a solution?

FYI: The array I am working with returns:

array(31) { 
[0]=>  array(5)
     { ["id"]=>  string(2) "31" ["title"]=>  string(4) "Home" ["linkable"]=>  string(1) "1" ["parent"]=>  NULL ["override"]=>  string(1) " " } 
[1]=>  array(5)
     { ["id"]=>  string(2) "30" ["title"]=>  string(11) "Shop Online" ["linkable"]=>  string(1) "1" ["parent"]=> string(2) "31" ["override"]=>  string(4) "shop" } 

and on and on.
  • 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-16T08:45:13+00:00Added an answer on May 16, 2026 at 8:45 am

    You need to write a recursive function to do this and have it call itself. I haven’t tested this out, but I think it should get you started. I wouldn’t endorse this function’s efficiency since it runs through every item in the array and does a comparison even though you’re going to only need an item or two from each run (likely).

    PHP:

    $arr = array(...);
    function output_lis($parentID = NULL){
        global $arr;
        $stack = array(); //create a stack for our <li>'s 
        foreach($arr as $a){ 
            $str = '';
                //if the item's parent matches the parentID we're outputting...
            if($a['parent']===$parentID){ 
                $str.='<li>'.$a['title'];
    
                        //Pass this item's ID to the function as a parent, 
                        //and it will output the children
                $subStr = output_lis($a['id']);
                if($subStr){
                    $str.='<ul>'.$subStr.'</ul>';
                }
    
                $str.='</li>';
                $stack[] = $str;
            }
        }
        //If we have <li>'s return a string 
        if(count($stack)>0){
            return join("\n",$stack);
        }
    
        //If no <li>'s in the stack, return false 
        return false;
    }
    

    Then output this on your page. Something like:

    <ul>
        <?php echo output_lis(); ?>
    </ul>
    

    Here is my sample array:

    $arr = array(
            array('title'=>'home','parent'=>NULL,'id'=>1), 
            array('title'=>'sub1','parent'=>1,'id'=>2), 
            array('title'=>'sub2','parent'=>1,'id'=>3), 
            array('title'=>'about us','parent'=>NULL,'id'=>4), 
            array('title'=>'sub3','parent'=>4,'id'=>5), 
            array('title'=>'sub4','parent'=>4,'id'=>6), 
        );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the issue I am having: I have a large query that needs
Here is the situation : I have a form created by HtmlService, and several
Here is the problem that I am trying to solve. I have two folders
Here's a query that works fine: SELECT rowid as msg_rowid, a, b, c FROM
Here is my code (Say we have a single button on the page that
Here's my scenario - I have an SSIS job that depends on another prior
Here is the Javascript I currently have <script type=text/javascript> $(function() { $('.slideshow').hover( function() {
Here is the script I'm using, copied directly from Google: <script type=text/javascript> var _gaq
Here's the basic setup: I have a thin bar at the top of a
Here is my problem : I have a post controller with the action create.

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.