0

我正在尝试使用递归函数打印菜单。我使用了这些问题的代码示例:

具有递归功能的回声菜单树

从数据库表创建多维数组

该代码对我来说似乎是正确的并且在语法上是正确的,但结果是我得到了一个空白页。没有错误,但是这个 php 后面的 HTML 也没有出现。有人有想法吗?

$connect = mysqli_connect($db_host,$db_user,$db_password,$db_database)
or die ("Couldn't connect to server: ".mysqli_error());

$query = "SELECT * FROM categories";

$result = mysqli_query($connect,$query)
    or die ("Couldn't execute query: ".mysqli_error());

function menuPrint($categories, $parent = 'root', $level = 0)
{
    $menuList = '<ul>';
    foreach($categories as $index => $category)
    {
      if($category['Parent'] == $parent)
      {
        $menuList .= '<li id="'.$category['ID'].'" class="tier'.$level.'">'.$category['Name'];
        $check = $this->menuPrint($categories, $category['ID'], $level+1);
        if($check != '<ul></ul>')
            $menuList .= $check;
        $menuList .= '</li>';
      }
    }
    return $menuList . '</ul>';
}

$catgories = array();
while ($row = mysqli_fetch_assoc($result)) {
    $categories[] = array(
        'ID' => $row['categoryId'],
        'Name' => $row['categoryName'],
        'Parent' => $row['parentCategory'],
        'Disabled' => $row['disabled']
    );
}
$menu = $this->menuPrint($categories);
echo $menu;

mysqli_close($connect);
4

0 回答 0