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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:02:46+00:00 2026-05-25T16:02:46+00:00

My db table looks like that. I’m trying to generate nav. menu from db

  • 0

My db table looks like that. I’m trying to generate nav. menu from db table. Here is my tables screenshot

enter image description here

I wanna get something like that

<li><a href="?page=1">Level 1</a>
  <ul>
   <li><a href="?page=2">Level2</a>
     <ul>
       <li><a href="?page=3">Level3</a></li> 
     </ul>
   </li>
  </ul>
</li>

And here is my recursive php function that generates nav function.

<!doctype html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<?php
require 'core/includes/db.php';
function menu($parent, $level){
  global $db;
  $q = $db->query("select * from menu where parent = '$parent'");
  if($level > 0 && $q->num_rows > 0){
    echo '<ul>';
  }
  while($row=$q->fetch_object()){
    echo "<li>".$q->name."</li>"; (line 14)
    //display this level's children
    menu($q->id, $level+1); (line 16)
  }
  if($level > 0 &&  $q->num_rows > 0){
    echo '</ul>';
  }
}
echo '<ul>' . menu(0,0) . '</ul>'

?>

Actually it doesn’t generate anything. It works recursively and getting bunch of errors in php logs.

[15-Sep-2011 00:47:22] PHP Notice:  Undefined property: mysqli_result::$id in E:\Web Server\smiths-heimann.az\nav.php on line 16
[15-Sep-2011 00:47:22] PHP Notice:  Undefined property: mysqli_result::$name in E:\Web Server\smiths-heimann.az\nav.php on line 14

What’s wrong with my function? Please help to fix that

UPDATE
Final PHP CODE

<!doctype html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<?php
require 'core/includes/db.php';
function menu($parent, $level){
  global $db;
  $q = $db->query("select * from menu where parent = '$parent'");
  if($level > 0 && $q->num_rows > 0){
    echo '<ul>';
  }
while($row=$q->fetch_object()){
    echo "<li>";
    echo '<a href="?page=' . $row->id . '">' . $row->name . '</a>';
    //display this level's children
    menu($row->id, $level+1);
    echo "</li>\n";
}
  if($level > 0 &&  $q->num_rows > 0){
    echo '</ul>';
  }
}
echo '<ul>' . menu(0,0) . '</ul>'

?>


 </html>

OK. Now i’m getting what exactly what i want. Thank you Adam very much. But there are 3 pair of empty ‘s at the end of page. Why they are appearing? Please take a look

<!doctype html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

</head>

<li><a href="?page=1">Ana Səhifə</a></li> 
<li><a href="?page=2">Təftiş texnikası</a><ul><li><a href="?page=3">Təhlükə üzrə sıralanma</a></li> 
</ul></li> 
<li><a href="?page=4">Istifadə olunan texnologiyalar</a><ul><li><a href="?page=5">Qamma şüa spektroskopiyası</a></li> 
<li><a href="?page=6">Portativ Ion Spektrometri</a></li> 
<li><a href="?page=7">İnfra qırmızı</a></li> 
<li><a href="?page=8">Mikrodalğa</a></li> 
<li><a href="?page=9">Raman Spektroskopiyası</a></li> 
<li><a href="?page=10">Rentgen araşdırma sistemləri</a><ul><li><a href="?page=11">Məktub və Banderolların yoxlanışı</a></li> 
<li><a href="?page=12">HiTraX Texnologiyası</a></li> 
</ul></li> 
</ul></li> 
<ul></ul>



 </html>
  • 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-25T16:02:47+00:00Added an answer on May 25, 2026 at 4:02 pm

    You have $q and $row mixed up in both lines 14 and 16. Each time your while loop runs, it puts the current object (from $q->fetch_object()) into $row. I’ve also updated my answer to better match your required output in the (updated) question

    while($row=$q->fetch_object()){
        echo "<li>";
        echo '<a href="?page=' . $row->id . '">' . $row->name . '</a>';
        //display this level's children
        menu($row->id, $level+1);
        echo "</li>";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My db table looks like that I'm trying to get page title and content
I have a table that looks like that: The rows are sorted by CLNDR_DATE
I have a table that looks like: Client_ID | Order_ID | 10 | 1
I have a table that looks like this: nid vid tid 1 2 3
I have a table that looks like this: <table> <thead> <!-- random table headings
I have a table that looks like the following: <table class=theClass> <tr> <td class=anotherClass><strong>Label1:</strong></td>
I have a table that looks like this: |StreetName NR| NR | |Teststreet 34|
I have a table that looks like this: id count 1 100 2 50
I have a table that looks like this for about ~30 students: StudentID Course*
I'd like to create a table that looks like this: lolvalue---------|lol date|some other column

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.