当我运行此代码时:
foreach ($tree as $node) {
echo str_repeat(' ', $node->tree_depth * 4) . $node->id . PHP_EOL;
}
Run Code Online (Sandbox Code Playgroud)
我得到格式良好的文字,如:
Food
Fruit
Red
Cherry
Strawberry
Cool
Not cool
Yellow
Banana
Meat
Beef
Pork
Run Code Online (Sandbox Code Playgroud)
但我想创建一个列表<ul><li>...:
我尝试过:
echo '<ul>';
$prev_depth = 0;
foreach($table->fetchTree() as $row) {
if ($row->tree_depth > $prev_depth) {
echo '<li><ul>';
} else if ($row->tree_depth < $prev_depth) {
echo '</li></ul>';
}
echo '<li>' . $row->name . '</li>';
$prev_depth = $row->tree_depth;
}
echo '</ul>';
Run Code Online (Sandbox Code Playgroud)
但我有一些额外的ul标签等等.我失去了2天,如果你能帮助我,请在这里发帖...