2

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>
hakre
  • 193,403
  • 52
  • 435
  • 836
Tural Ali
  • 22,202
  • 18
  • 80
  • 129
  • Also, you should only be querying the database once and letting your recursion piece things together. Iteratively querying the database can be extremely intensive, especially as the depth and detail of your menu continues to grow. – Timothy Allyn Drake Sep 14 '11 at 19:04

1 Answers1

5

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>";
}
Adam Hopkinson
  • 28,281
  • 7
  • 65
  • 99
  • but it doesn't work like i mentioned. Updated question please take a look – Tural Ali Sep 14 '11 at 19:05
  • I've updated my answer. If it still doesn't work, actually provide useful feedback about what *is* happening. – Adam Hopkinson Sep 14 '11 at 19:09
  • It closes `` of parent items. It must be closed after for parent items. How can i prevent it? – Tural Ali Sep 14 '11 at 19:10
  • updated question, please take a look. And thx you very much, GOD bless you – Tural Ali Sep 14 '11 at 19:18
  • Try indenting your code and it will be more clear. Hint: you are missing an opening `
      `. If my answer answered the original question, please mark it as accepted. If you have another issue, it's probably another question.
    – Adam Hopkinson Sep 14 '11 at 19:27
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3455/discussion-between-tural-teyyuboglu-and-adam) – Tural Ali Sep 14 '11 at 19:37