gpt4 book ai didi

php - 递归 php 函数问题 : works without any result

转载 作者:太空宇宙 更新时间:2023-11-04 15:22:26 25 4
gpt4 key购买 nike

我的数据库表看起来像那样。我正在尝试生成导航。数据库表中的菜单。这是我的表格截图

enter image description here

我想要这样的东西

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

最佳答案

您在第 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>";
}

关于php - 递归 php 函数问题 : works without any result,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7421530/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com