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

  • Home
  • SEARCH
  • 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 8351553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:39:54+00:00 2026-06-09T08:39:54+00:00

My objective is to create navigation menu that has unlimited depth tree. I can

  • 0

My objective is to create navigation menu that has unlimited depth tree. I can successfully generate the top 2 levels. The first (Top level) is using a PHP array that is not stored within the XML file. The second is generated from a flat xml file. The main problem is successful recursion.

Here is the XML structure:

<articles>
    <article>
        <menu parent="Insurance" label="Business" target="#" show="true" />
        <data />
        <body />
    </article>
    <article>
        <menu parent="Resources" label="Videos" target="content.php#videos" show="true" />
        <data />
        <body />
    </article>
    <article>
        <menu parent="Business" label="Disability" target="content.php#disability" show="true" />
        <data />
        <body />
    </article>
    <article>
        <menu parent="Business" label="Liability" target="content.php#liability" show="true" />
        <data />
        <body />
    </article>

</articles>

I am using # in my target attribute to identify if a new list (sub-menu) needs to be started.

The PHP code that I receive success with outputting the correct information is:

    $objXMLMenu = simplexml_load_file('menu.xml');
    foreach ($mainmenu as $menuparent) {  //Main Array

        echo "<li><a href=\"" .$maintargets[$i]. "\">". $menuparent ."</a>\n";  // Top Menu array. I use 2 for clarity
        if ($maintargets[$i] == "#"){


            //Open the ULs for filling
            echo "\t<ul>\n";

    // Start function here?

            foreach($objXMLMenu->article as $art){

                foreach($art->menu as $menuitem){

                    if($menuitem['parent'] == $menuparent){ //Compare to Main Array

                        if ($menuitem['show'] == "true"){

                            if($menuitem['target'] == "#"){  //A Submenu exits here
                                echo "\t\t<li><a href=\"" . $menuitem['target'] . "\" >" .$menuitem['label'] . "</a></li>\n";
                                echo "\t\t\t<ul>\n";
                                //Run XMLQUERY match? As a 

                                echo "\t\t\t</ul>\n";
                            }else{
                                echo "\t\t<li><a href=\"" . $menuitem['target'] . " \"rel=\"ajax\">" .$menuitem['label'] . "</a></li>\n";
                            }
                        }

                    }
                }

            }
            //Close Middle Menus
            echo "\t</ul>";
        }
        $i++;
        //close Top Level Menu Item
        echo "</li>\n";
    }
//<UL> Footer
echo "\t</ul>\n</div>\n";

In an attempt to make it a recursive function I receive an error with my 1st foreach statement, which suggests to me I should switch to DOM & XPATH instead of simplexml. Here is the function I’ve come up with so far:

$objXMLMenu = simplexml_load_file('menu.xml');
// Start function here!
function mysubmenu($menuparent){
    foreach($objXMLMenu->article as $art){
        foreach($art->menu as $menuitem){
            if($menuitem['label'] == $menuparent){
            //Compare to Main Array
                if ($menuitem['show'] == "true"){

                    if($menuitem['target'] == "#"){
                        // ((ROOT)
                        //A Submenu exits here
                        $strResponse .= "\t\t<li><a href=\"" . $menuitem['target'] . " \">" .$menuitem['label'] . "</a></li>\n";
                        $strResponse .= "\t\t\t<ul>\n";

                        //xmlpath QUERY instead?
                        mysubmenu($menuitem['parent']);

                        $strResponse .= "\t\t\t</ul>\n";
                    }else{
                        $strResponse .= "\t\t<li><a href=\"" . $menuitem['target'] . " \"rel=\"ajax\">" .$menuitem['label'] . "</a></li>\n";
                    }
                }

            }
        }
        //insert counter to stop foreach loop after all records are posted.
    }
    return $strResponse; 
} //End Function

Recursion of this type is a new boundary for me. Every example or explanation I have found involves XML tree’s going deeper into the elements. A “depth” (counter) attribute is not an option. Since, I am facing a similar problem on a “postponed” project that truly is unlimited depth over time.

  • 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-06-09T08:39:57+00:00Added an answer on June 9, 2026 at 8:39 am

    What worked on the procedural style did not work in the function. My most troubling aspect was in comparing SimpleXML objects. There are about 5 lines that needed to be changed. There is probably a simpler and faster way to get to this answer though for longer recursions. But I found this works for now.

    function mysubmenu($menuparent){
        $objXMLMenu = simplexml_load_file('menu.xml');
        foreach($objXMLMenu->article as $art){
            foreach($art->menu as $menuitem){
    
                if((string)$menuitem['parent'] == $menuparent){ 
                    if ($menuitem['show'] == "true"){
                        if($menuitem['target'] == "#"){
                            echo "\t\t<li><a href=\"" . $menuitem['target'] . "\">" .$menuitem['label'] . "</a></li>\n";
                            echo "\t\t\t<ul>\n";
                            mysubmenu($menuitem['label']);
                            echo "\t\t\t</ul>\n";
                        }else{
                            echo "\t\t<li><a href=\"" . $menuitem['target'] . "\" rel=\"ajax\">" .$menuitem['label'] . "</a></li>\n";
                        }
                    }
    
    
                }
            }
    
        }
    
    } //End Function 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After Python Objective-C syntax blows my mind! I'm trying to create a menu that
My end objective is to create an applescript that intelligently inserts a bullet point
I need to create an Objective-C method that converts an int into a byte
I'm trying to create an objective C classe for my iPad application which can
Objective: I want to create a web service that allows me to connect to
My objective is create an apache module that will provide RESTful services (i.e., we
I am trying to create an objective c interface that encapsulates the functionality of
I am trying to create this dropdown box that slides down and has the
I want to create an Objective-C base class that performs an operation on all
The objective is to create a custom route so that /undead will go to

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.