【问题标题】:How to print list using hierarchical data structure?如何使用分层数据结构打印列表?
【发布时间】:2009-05-23 13:58:14
【问题描述】:

当我运行这段代码时:

foreach ($tree as $node) {
    echo str_repeat(' ', $node->tree_depth * 4) . $node->id . PHP_EOL;
}

我得到格式良好的文本,例如:

Food
 Fruit
   Red
     Cherry
     Strawberry
               Cool
               Not cool
   Yellow
     Banana
 Meat
   Beef
   Pork

但我想用<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>';

但是我有一些额外的 ul 标签等等。我为此浪费了 2 天,所以如果你能帮助我,请在此处发布...

【问题讨论】:

标签: php list hierarchy


【解决方案1】:

试试这个算法:

$tree = array(
    array('Food', 0),
    array('Fruit', 1),
    array('Red', 2),
    array('Cherry', 3),
    array('Strawberry', 3),
    array('Cool', 4),
    array('Not cool', 4),
    array('Yellow', 2),
    array('Banana', 3),
    array('Meat', 0),
    array('Beef', 1),
    array('Pork', 1),
);

$depth = -1;
$flag = false;
foreach ($tree as $row) {
    while ($row[1] > $depth) {
        echo "<ul>\n", "<li>";
        $flag = false;
        $depth++;
    }
    while ($row[1] < $depth) {
        echo "</li>\n", "</ul>\n";
        $depth--;
    }
    if ($flag) {
        echo "</li>\n", "<li>";
        $flag = false;
    }
    echo $row[0];
    $flag = true;
}
while ($depth-- > -1) {
    echo "</li>\n", "</ul>\n";
}

在这里,您只需将$tree 替换为$table-&gt;fetchTree(),将$row[0] 替换为$row-&gt;name$row[1] 替换为$row-&gt;tree_depth

【讨论】:

  • Gumbo,信不信由你我发现你的算法有问题,但不幸的是,我无法解决它,问题是你的算法没有生成有效的 ul li 结构,你有一些额外的非必要的标签,你能解决它吗!?
  • 我没有看到任何错误。你有输出无效的例子吗?
  • 是的,我确实这样做了,你想让我怎么发送它!?如果你愿意,我也可以给你发电子邮件。这是输出,在浏览器中看起来不错,但是如果您分析源代码,您将看到问题jsbin.com/oliya
  • 代码中有 &lt;/l&gt; 而不是 &lt;/li&gt;。但我是正确的。
  • 那么这行中的&lt;/l&gt;是从哪里来的呢?见validator.w3.org/…
【解决方案2】:

试试这个代码:

<?php
echo "<ul>\n";

$tree = array(
    array('Food', 0),
    array('Fruit', 1),
    array('Red', 5),
    array('Cherry', 3),
    array('Strawberry', 3),
    array('Cool', 4),
    array('Not cool', 4),
    array('Yellow', 2),
    array('Banana', 3),
    array('Meat', 0),
    array('Beef', 4),
    array('Pork', 2),
);

$depth = 0;

foreach ($tree as $node) {
  if ($node[1] > $depth)
    echo str_repeat("<ul>\n", $node[1] - $depth);
  if ($node[1] < $depth)
    echo str_repeat("</ul>\n", $depth - $node[1]);
  $depth = $node[1];

  echo "<li>" .  $node[0] . "\n";
}
echo str_repeat("</ul>\n", $depth+1);
?>

我已对其进行了更新以输出更少的&lt;li&gt; 标签,从而减少了项目符号的数量。但另一方面,这将生成不会验证的 HTML,因为超过一级的跳转会导致生成 &lt;ul&gt;&lt;ul&gt;

【讨论】:

  • 伙计,这给我的输出与我在问题中发布的代码几乎相同,所以这不是解决方案......
  • 很难看出你真正在问什么......我认为你的问题是我的代码处理的不止一个级别的跳转。
  • 我认为,我的问题可能并不完全清楚,但我发布的代码就像牛奶一样清晰:D 无论如何,谢谢你的帮助,我很感激,真的......
  • 太好了,我很高兴听到这个消息!
猜你喜欢
  • 2021-12-14
  • 2021-06-21
  • 1970-01-01
  • 2020-11-27
  • 1970-01-01
  • 2012-08-24
  • 1970-01-01
  • 2015-02-24
  • 1970-01-01
相关资源
最近更新 更多