0

我正在尝试使用 PHP 和 MySQL(通过 CodeIgniter,尽管我认为这不相关)创建一个嵌套的无序列表。

我已经看到许多似乎适用于具有两级嵌套的列表的解决方案,但我需要的解决方案必须具有三个级别。这是我需要的东西:

<ul>
    <li>Top Level, Item 1
        <ul>
            <li>Second Level, Item 1
                <ul>
                    <li>Third Level, Item 1</li>
                    <li>Third Level, Item 2</li>
                    <li>Third Level, Item 3</li>
                </ul>
            <li>Second Level, Item 2
                <ul>
                    <li>Third Level, Item 4</li>
                    <li>Third Level, Item 5</li>
                    <li>Third Level, Item 6</li>
                </ul>
            </li>
        </ul>
    </li>
</ul>

我的数据库的输出基本上是:

TOP LEVEL | SECOND LEVEL | THIRD LEVEL
Item 1 | Item 1 | Item 1
Item 1 | Item 1 | Item 2
Item 1 | Item 1 | Item 3
Item 1 | Item 2 | Item 4
Item 1 | Item 2 | Item 5
Item 1 | Item 2 | Item 6

我尝试过查看数据库的输出,使用变量来注册我所在的级别等,但我在关闭每个列表时陷入了可怕的混乱。

任何人都可以建议这样做的方法吗?

4

2 回答 2

0

感谢所有的帮助,但我想我自己已经破解了它,以完全不同的方式!

@DevishOne 提供的解决方案让我开始思考,并使用它作为一个模板,我添加了变量来确定是否应该打开或关闭新的子列表或子子列表。

我不是说这是万无一失的,我也不是说它特别有效,但它对我有用(到目前为止!)

我的解决方案:

//Placeholder variables
$current_top = '';
$current_second = '';
$current_third = '';

echo '<ul>';
foreach($array as $row) {
    //If it is a new top level item...
    if ($row['top_level'] != $current_top) {
        //If it isn't the very first top level item...
        if ($current_top != '') {
            //Close off all of the other first, second, and third level branches.
            echo '</ul></li></ul></li>';
        }
        //Echo the top level item, including the start of the sub-list.
        echo '<li>'.$row['top_level'].'<ul>';
        //Set the current top level variable to the current top level item.
        $current_top = $row['top_level'];
        //Set the current second level variable back to null (if it isn't already).
        $current_second = '';
    } 
    //If it is a new second level...
    if ($row['second_level'] != $current_second) {
        //If it isn't the very first second level item...
        if ($current_second != '') {
            //Close off all of the other second and third level branches.
            echo '</ul></li>';
        }
        //Echo the second level item, including the start of the sub-sub-list.
        echo '<li>'.$row['second_level'].'<ul>';
        //Set the current second level variable to the current second level item.
        $current_second = $row['second_level'];
        //Set the current third level variable back to null (if it isn't already).
        $current_third = '';
    } 
    //If it is a new third level...
    if ($row['third_level'] != $current_third) {
        //Echo the third level item, and close it off.
        echo '<li>'.$row['third_level'].'</li>';
        //Set the current third level variable to the current third level item.
        $current_third = $row['third_level'];
    }
}
echo '</ul>';
于 2013-08-06T13:11:39.853 回答
0

询问

SELECT a.item, b.item, c.item FROM TOP_LEVEL a JOIN SECOND_LEVEL b ON b.level = a.level JOIN THIRD_LEVEL c ON c.level = b.level ORDER BY a.item, b.item, c.item

代码

<?php
$q = "SELECT a.item, b.item, c.item FROM TOP_LEVEL a JOIN SECOND_LEVEL b ON b.level = a.level JOIN THIRD_LEVEL c ON c.level = b.level ORDER BY a.item, b.item, c.item ";
...... your database stuff......
...... resulting array should be two-dimensional `$items` ......
$c = 0;
echo "<ul>\n";
while ($c < count($items)) {
    for ($j = 0; $j < count($items[$c][0]); $j++) {
        echo "<li>$items[$c][0]\n";
        echo "<ul>\n";
        for ($k = 0; $k < count($items[$c][1]); $k++) {
            echo "<li>$items[$c][1]\n";
            echo "<ul>\n";
            for ($i = 0; $i < count($items[$c][2]); $i++) {
                echo "<li>$items[$c][2]<\li>\n";
                $c++;
            }
            echo "</ul>\n";
            echo "</li>\n";
                    $c++;
        }
        echo "</ul>\n";
        echo "</li>\n";
            $c++;
    }
    echo "</ul>\n";
}
?>
于 2013-08-05T16:45:56.793 回答