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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:13:06+00:00 2026-05-20T14:13:06+00:00

I have been messing around with recursion a few hours and just cant get

  • 0

I have been messing around with recursion a few hours and just cant get it right!

I want my menu structure to be:

<ul>
   <li> menu 1 <ul>
      <li> menu 1.1 <ul>
         <li> menu 1.1.1 </li>
      </ul>
   </ul>
<li> menu 2 </li>
<li> menu 3 <ul>
   <li> menu 3.1 </li>
</ul>
<li> menu 4 </li>
</ul>
</li>

but i just cant get it right.

    print '<ul>';
    display_children(0, 0);
function display_children($parent, $level) {
   // retrieve all children of $parent

   $result = mysql_query('SELECT * FROM kategori WHERE parent="'.$parent.'";');

   while ($row = mysql_fetch_array($result)) {

       if($row['parent']!=0)
            print '<ul>';
       print '<li>';

       // indent and display the title of this child
       echo $row['navn']."\n";

       // call this function again to display this
       // child's children
       display_children($row['id'], $level+1);

   }

        print '</li>';
           print '</ul>';

as it is now, it prints the right structure but poops out a crapton of missplaced <ul> and <il>

my top level menu items have 0 as parent (to indicate that they are the absolut top) and its children have their respective parents id as parent attribut.

i tried to check if the current run through of the function (lets say we were at menu 1.1.1) had parent 0 which, to my surprise, was true as i would have thought the current run through would have have parent “menu 1.1” instead of “menu 1”

Any one able to help me out here? I have been staring at that damn code for waaay to long and if you have a quick fix to how i can get this code snippet to insert UL and IL the right places i would be most grateful!

Thanks in advance, Rasmus

  • 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-20T14:13:07+00:00Added an answer on May 20, 2026 at 2:13 pm

    display_children is perhaps not the best name, something like display_menu_level might be more descriptive. Think about what it has to do: display a <ul>, then a number of children, then a </ul>. Each child is an <li>, the “navn” text, a submenu, and an </li>. Just reflect that structure in code, something like this:

    function display_menu_level($parent, $level) {
        $results = mysql_query('SELECT * FROM kategori WHERE parent="'.$parent.'";');
    
        // Don't print an empty list ("<ul></ul>")
        if (mysql_num_rows($results) == 0) return;
    
        // Start the menu
        echo '<ul>';
    
        // Display children
        while ($row = mysql_fetch_array($result)) {
            // Start this child
            print '<li>';
    
            // indent and display the title of this child
            echo $row['navn']."\n";
    
            // call this function again to display this
            // child's children
            display_children($row['id'], $level+1);
    
            // Close this child
            print '</li>';
        }
    
        // Close the menu
        print '</ul>';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.