【问题标题】:Recursive php function issue: works without any result递归php函数问题:没有任何结果
【发布时间】:2011-11-17 07:16:51
【问题描述】:

我的数据库表看起来像这样。我正在尝试生成导航。 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>

这是我生成导航函数的递归 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>'

?>

实际上它不会产生任何东西。它以递归方式工作,并在 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

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

更新 最终 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>

好的。现在我得到了我想要的。非常感谢亚当。但是页面末尾有 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>

【问题讨论】:

  • 另外,您应该只查询数据库一次,然后让您的递归将所有内容拼凑在一起。迭代查询数据库可能会非常密集,尤其是在菜单的深度和细节不断增长的情况下。

标签: php html mysql mysqli


【解决方案1】:

在第 14 行和第 16 行中,$q$row 混合在一起。每次运行 while 循环时,它会将当前对象(来自 $q-&gt;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>";
}

【讨论】:

  • 但它不像我提到的那样工作。更新的问题请看一下
  • 我已经更新了我的答案。如果它仍然不起作用,请实际提供有关正在发生的事情的有用反馈。
  • 关闭父项的&lt;/li&gt;。对于父项,它必须在 之后关闭。我该如何预防?
  • 更新问题,请看一下。非常感谢你,上帝保佑你
  • 尝试缩进你的代码,它会更清晰。提示:您缺少一个开头&lt;ul&gt;。如果我的回答回答了原始问题,请将其标记为已接受。如果您有其他问题,可能是另一个问题。
猜你喜欢
  • 2017-10-18
  • 1970-01-01
  • 1970-01-01
  • 2021-05-17
  • 2015-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多