1

目前我正在使用递归函数以文本格式显示数据,例如..

Food    
    Fruit
        Red
            Apple
        Yellow
            Banana
    Meat
        Beef
        Pork

我正在使用以下代码来显示此文本

function display_children($parent, $level) { 
    $result = mysql_query('SELECT * FROM category '.
    'WHERE parant_id="'.$parent.'";'); 

    while ($row = mysql_fetch_array($result)) { 
        echo str_repeat('    ',$level).$row['name']."\n"; 
        display_children($row['id'], $level+1); 
    } 
} 

display_children(0,0);

<ul> <li>但现在我想在菜单中显示上述文本

<ul>
    <li>Food
        <ul>
            <li>Fruit
                <ul>
                    <li>Red
                        <ul>
                            <li>Apple</li>
                        </ul>
                    </li>
                    <li>Yellow
                        <ul>
                            <li>Banana</li>
                        </ul>
                    </li>
                </ul>
            </li>
            <li>Meat
                <ul>
                    <li>Beef</li>
                    <li>Pork</li>
                </ul>
            </li>
        </ul>
    </li>
</ul>

所有数据都以以下格式存储在表中

+----+-----------+--------+
| id | parant_id |  name  |
+----+-----------+--------+
|  1 |         0 | Food   |
|  2 |         1 | Fruit  |
|  3 |         1 | Meat   |
|  4 |         2 | Red    |
|  5 |         2 | Yellow |
|  6 |         4 | Apple  |
|  7 |         5 | Banana |
|  8 |         3 | Beef   |
|  9 |         3 | Pork   |
+----+-----------+--------+

请帮我以<ul> <li>格式显示整个数据

谢谢

4

2 回答 2

3
// pass a full row to the function instead of only the id
// also, with this solution you don't need the level, but I kept it there, you can use it for styling, etc.
function display_with_children($parentRow, $level) { 
    echo '<li>'.$parentRow['name']; 
    // if your id column is integer, you don't need the quotation mark
    $result = mysql_query('SELECT * FROM category WHERE parant_id='.$parentRow['id'].';');
    if (mysql_num_rows($result) != 0) {
        echo '<ul>';
        // use the fetch_assoc to get an associative array
        while ($row = mysql_fetch_assoc($result)) { 
            display_with_children($row, $level+1); 
        } 
        echo '</ul>';
    }
    echo '</li>';
} 

并像使用它一样

$rootRow = ....;
echo '<ul>';
display_with_children($rootRow, 0);
ehco '</ul>;

编辑:请注意,这个解决方案是相当理论上的,因为它有性能问题(见评论)。如果您没有太多类别,最好从数据库中将它们全部一起获取,并从内存中递归处理它们,这将是类似的东西,除了您检索每个类别的子项的方式。如果您有大量的嵌套类别或标签,并且您通常需要处理它们的部分树,您可以考虑使用嵌套集合模型

于 2013-06-23T10:29:59.707 回答
2

我更喜欢只执行一个 mysql 查询,然后尽量避免递归(因为内存使用)。输出是一个数组,其中所有项目都是有序的 => 子元素始终跟随其父元素。非常有用的是childrenwhere 是所有直接子级的列表。只有一个限制:每个父母的最大子女数为 99,因为$digits=2;. 您可以将其更改为$digits=3;并将加注计数为 999 ;)

数据库记录:

id  parent  name
1     0     Food
2     1     Fruit
3     2     Red
4     3     Apple
5     2     Yellow
6     5     Banana
7     1     Meat
8     7     Beef
9     7     Pork

代码:

$query = "SELECT * FROM `menu` ORDER BY `parent`";
$result = mysqli_query($dbc, $query);
$data = array();
while ($row = mysqli_fetch_assoc($result)) {
    $data[$row['id']] = $row;
}


/******************* ARRAY LIST GENERATOR *******************/
$digits = 2;
$menu = array();
$added = 1;
while ($added) {
    $added = 0;
    foreach ($data as $k => &$v) {
        if (!isset($v['key'])) {
            if (!$v['parent']) {
                $key = sprintf("%0{$digits}d", ++$added);
                $menu[$key] = $v;
                $v['key'] = $key;
                $menu[$key]['parentKey'] = 0;
                $menu[$key]['level'] = 1;
            } else {
                if (isset($data[$v['parent']]['key']) && isset($menu[$data[$v['parent']]['key']])) {
                    if (isset($menu[$data[$v['parent']]['key']]['children'])) {
                        $key = $data[$v['parent']]['key'] . sprintf("%0{$digits}d", count($menu[$data[$v['parent']]['key']]['children']) + 1);
                        $menu[$data[$v['parent']]['key']]['children'][] = $key;
                    } else {
                        $key = $data[$v['parent']]['key'] . sprintf("%0{$digits}d", 1);
                        $menu[$data[$v['parent']]['key']]['children'][] = $key;
                    }
                    $menu[$key] = $v;
                    $menu[$key]['parentKey'] = $data[$v['parent']]['key'];
                    $menu[$key]['level'] = $menu[$menu[$key]['parentKey']]['level'] + 1;
                    $v['key'] = $key;
                    $added++;
                }
            }
        }
    }
}

ksort($menu, SORT_STRING);

/******************* UL LI GENERATOR *******************/
$li = array();
$ul = array();
$levelLL = 0;
echo "<ul>\n";
$ul[1] = 1;
foreach ($menu as $k => &$v) {
    if ($v['level'] < $levelLL) {
        for ($i = $levelLL; $i > $v['level'] - 1; $i--) {
            if (isset($li[$i])) {
                echo "</li>\n";
                unset($li[$i]);
            }
            if (isset($ul[$i]) && $i > $v['level']) {
                echo "</ul>\n";
                unset($ul[$i]);
            }
        }
    } elseif ($v['level'] == $levelLL) {
        echo "</li>\n";
    }
    echo "<li>\n";
    $li[$v['level']] = 1;
    echo $v['name'] . "\n";
    if (isset($v['children'])) {
        echo "<ul>\n";
        $ul[$v['level'] + 1] = 1;
    }
    $levelLL = $v['level'];
}
if (0 < $levelLL) {
    for ($i = $levelLL; $i > -1; $i--) {
        if (isset($li[$i])) {
            echo "</li>\n";
            unset($li[$i]);
        }
        if (isset($ul[$i]) && $i > 0) {
            echo "</ul>\n";
            unset($ul[$i]);
        }
    }
}

输出:

<ul>
    <li>
        Food
        <ul>
            <li>
                Fruit
                <ul>
                    <li>
                        Red
                        <ul>
                            <li>
                                Apple
                            </li>
                        </ul>
                    </li>
                    <li>
                        Yellow
                        <ul>
                            <li>
                                Banana
                            </li>
                        </ul>
                    </li>
                </ul>
            </li>
            <li>
                Meat
                <ul>
                    <li>
                        Beef
                    </li>
                    <li>
                        Pork
                    </li>
                </ul>
            </li>
        </ul>
    </li>
</ul>
于 2013-06-23T11:57:05.980 回答