我几个小时都在苦苦挣扎,在网上找不到任何信息。 我有一个create_header函数,它输出html然后使用递归函数(称为render_tree)从数据库动态创建类别列表。此函数使用echo输出相关的html。
当我尝试使用create_header函数运行此函数时,它不会输出任何内容。我已经尝试将render_tree函数包含在create_header函数中以确保它是可访问的,但显然我仍然做错了
create_header()
{
?> <html>...
<?php
$conn = db_connect();
function renderTree($parent = "0")
{
//Category Menu - Main recursive function. I'll asume '0' id is the root node
global $categoryNames;
global $childrenTree;
$children = $childrenTree[$parent];
if($parent != "0")
{
if ($categoryNames[$categoryNames[$parent]] == 1)
{
if (count($children) == 0 && $categoryNames[$categoryNames[$parent]] == 2)
echo "<a class='subexpandable' href='item_list.php?cat=$parent'>" . $categoryNames[$parent] . "</a>\n";
if (count($children) == 0 && $categoryNames[$categoryNames[$parent]] == 1)
echo "<a class='menu' href='item_list.php?cat=$parent'><h3 class='menuheader'>" . $categoryNames[$parent] . "</h3></a>\n";
else
echo "<h3 class='menuheader expandable'>" . $categoryNames[$parent] . "</h3>\n";
}
else
{
if (count($children) == 0)
echo "<li><a href='item_list.php?cat=$parent'>", $categoryNames[$parent], "</a></li>\n";
else
echo "<li><a class='subexpandable' href='item_list.php?cat=$parent'>", $categoryNames[$parent], "</a></li>\n";
}
}
if(count($children) > 0){ //If node has children
if ($parent == "0")
echo "<div class='arrowlistmenu'>\n";
else
{
if ($categoryNames[$categoryNames[$parent]] == 2)
echo "<ul style='margin-left: 15px' class='subcategoryitems'>\n";
elseif ($categoryNames[$categoryNames[$parent]] == 1)
echo "<ul class='categoryitems'>\n";
}
foreach($children as $child)
renderTree($child);
echo "</ul>\n";
}
//if($parent != "0") echo "</li>\n";
if($parent == "0") echo "</div>";
}
$result = $conn->query("SELECT category_id AS id, parent_category_id AS parent_id, category_name AS category, level AS level FROM sp_categories WHERE is_showing = 1 ORDER BY `order`,category_id");
$childrenTree = array(); //Will store an array of children for each parent
$categoryNames = array(); //Will store category name for each id
//We fill $childrenTree and $categoryNames from database
while ($row = mysqli_fetch_array($result)){
list($id, $parent_id, $category, $level) = $row;
$categoryNames[(string)$id] = $category;
$categoryNames[(string)$category] = $level;
//$categoryNames[(string)$pid] = $parent_id;
$parent_id = (string)$parent_id;
// echo $parent_id;
if(!array_key_exists($parent_id, $childrenTree))
$childrenTree[$parent_id] = array();
$childrenTree[$parent_id][] = (string)$id;
}
renderTree(); //This renders the hierarchical tree
?>
<more html>
$parent_id
底部附近的回声正确输出ID,所以我知道它正在与数据库通信。
编辑:最初,我在html_fns.php库中有create_header函数,而php_fns.php库中的render_tree函数试图分离那些更多显示html的函数,这些函数包含大量的php他们。这与将render_tree嵌套在create_header中的结果相同 - 我嵌套它以尝试确保它被定义。
干杯, 克里斯
答案 0 :(得分:1)
在我看来,因为你在另一个函数中定义了一个函数(在PHP中很少有好主意),你的变量作用域有点搞砸了。 (以这种方式定义renderTree()并没有给它任何特殊的范围,它只是延迟定义它直到代码中的那一点,这对任何事都没有区别。)
具体来说,您定义$ childrenTree的部分位于create_header()函数的范围内,因此这不是全局变量,并且无法在renderTree()内部访问。
我认为更好的代码结构将使调试变得更容易:
// Generally considered a good idea to prepare data before display
$childrenTree = get_nodes_from_db();
// At this point, you shouldn't need to access the DB any more
display_header($childrenTree);
function display_header($childrenTree)
{
// ...
renderTree($childrenTree);
// ...
}
如果你绝对坚持为$ childrenTree使用全局变量而不是以这种方式传递它,将代码分成这三个函数,没有特技嵌套,并在get_nodes_from_db()的顶部添加全局$ childrenTree和renderTree()