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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:30:08+00:00 2026-05-26T04:30:08+00:00

Recently I’ve written recursive PHP function which generates website navigation based on parent-child structure

  • 0

Recently I’ve written recursive PHP function which generates website navigation based on parent-child structure like this

<ul>
  <li>parent
     <li>child</li>
  </li>
</ul>

Code looks like that

function generateMenu($parent, $level, $db){
  $q = $db->query("select id, name FROM menu WHERE parent = '$parent'");

  if($level > 0 && $q->num_rows > 0) echo "\n<ul>\n";

   while($row=$q->fetch_object()){
      echo "<li>";
      echo '<a href="?page=' .$page. '">' . $row->name . '</a>';
      //display this level's children
      generateMenu($row->id, $level++, $menu, $db);
      echo "</li>\n\n";
    }

    if($level > 0 &&  $q->num_rows > 0) echo "</ul>\n";
}

The piece of code above simply echoes <ul><li> structure (like given above example) from db table.

Now the questions is, how to create navigation menu like on this website?

Please take a look at left sidebar.

http://www.smithsdetection.com/continuous_vapour_sampling.php

Now i think that:

  1. First of all we need to echo all parents
  2. Function must get current pages id as an input value (for ex. $current)
  3. Function must echo til’ current pages level

I can’t figure out how to modify my function, to get output like on given website. PLease help.

BTW

My db table looks like that

enter image description here

NOTE Please don’t post answers about sql injection holes, I’ve already taken care about them: checking with in_array (if variable listed in column names array) and passing through real_escape.

  • 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-26T04:30:09+00:00Added an answer on May 26, 2026 at 4:30 am

    Assuming the current page id is in the var $current and that $db is an open MySQLi DB connection:

    // first get your current page's path back to root:
    // $stack will be a stack of menus to show
    $stack = array();
    
    // always expand current menu:
    $stack[] = $current;
    
    // now starting at $current, we go through the `menu` table adding each parent
    // menu id to the $stack until we get to 0:
    $i = $current;
    while ( $i > 0 ) {
      // get parent of $i
      $query = sprintf('SELECT `parent` FROM `menu` WHERE id=%d LIMIT 1', $i);
      $result = $db->query($query);
    
      if (!$result) {
        // do error handling here
      }
    
      $row = $result->fetch_assoc();
    
      // save parent id into $i...
      $i = $row['parent'];
    
      // ...and push it onto $stack:
      $stack[] = $i;
    }
    
    /**
     * @param int $parent the parent ID of the menu to draw.
     * @param array $stack the stack of ids that need to be expanded
     * @param string $indent string for pretty-printing html
     * @param MySQLi $db Open db connection
     */
    function generateMenu($parent, $stack, $indent, $db){
    
      // $next is the next menu id that needs expanding
      $next = array_pop($stack);
    
      $query = sprintf('SELECT `id`, `name` FROM `menu` WHERE `parent`=%d', $parent);
    
      $result = $db->query($query);
    
      if ( ! $result ) {
        // do error handling here
      }
    
      if ($result->num_rows > 0) {
        echo "\n$indent<ul>\n";
    
        while($row = $result->fetch_object()){
          echo "$indent  <li>\n";
          echo "$indent    <a href=\"?page={$row->id}\">{$row->name}</a>\n";
    
          //display this level's children, if it's the $next menu to need to be drawn:
          if ($row->id == $next)
            generateMenu($next, $stack, "$indent    ", $db);
    
          echo "$indent  </li>\n\n";
        }
        echo "$indent</ul>\n";
      }
      $result->free();
    }
    
    $first = array_pop($stack); // should always be 0
    generateMenu($first, $stack, '', $db);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently i've switched to PHP 5.3+ and after that migration i learned that the
recently I've installed the FireGestures plugin for Firefox, which I find very useful. You
recently I became interested in the subset-sum problem which is finding a zero-sum subset
Recently, I've been involved in two projects. The first one is built on php
Recently Jeff has posted regarding his trouble with database deadlocks related to reading. Multiversion
Recently I have been investigating the possibilities of caching in ASP.NET. I rolled my
Recently, I started changing some of our applications to support MS SQL Server as
Recently, I've been dealing with an error with accessing MAPI via the .NET framework
Recently our site has been deluged with the resurgence of the Asprox botnet SQL
Recently I had to develop a SharePoint workflow, and I found the experience quite

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.