递归的php函数问题:没有任何结果

Tur*_*Ali 2 html php mysql mysqli

我的db表看起来像那样.我正在尝试生成导航.db表中的菜单.这是我的表截图

在此输入图像描述

我想得到类似的东西

<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>
Run Code Online (Sandbox Code Playgroud)

这是我的递归php函数,它生成导航功能.

<!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>'

?>
Run Code Online (Sandbox Code Playgroud)

实际上它不会产生任何东西.它以递归方式工作并在php日志中获得大量错误.

[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
Run Code Online (Sandbox Code Playgroud)

我的功能出了什么问题?请帮忙解决这个问题

更新 最终的PHP代码

<!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>
Run Code Online (Sandbox Code Playgroud)

好.现在我正在得到我想要的东西.非常感谢亚当.但页面末尾有3对空.为什么他们出现?请看一下

<!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>
Run Code Online (Sandbox Code Playgroud)

Ada*_*son 5

第14行和第16行都有$q并且$row混合了.每次while循环运行时,它都会将当前对象(从$q->fetch_object())放入$row.我还更新了我的答案,以更好地匹配(更新)问题中的所需输出

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>";
}
Run Code Online (Sandbox Code Playgroud)