0
我使用这一类的脚本:如何在PHP中执行这种类型的增量?
<?php
include("connect.php");
$nav_query = mysql_query("SELECT * FROM `categories` ORDER BY `id`");
$tree = "";
$depth = 1;
$top_level_on = 1;
$exclude = array();
array_push($exclude, 0);
while ($nav_row = mysql_fetch_array($nav_query)) {
$goOn = 1;
for ($x = 0; $x < count($exclude); $x++) {
if ($exclude[$x] == $nav_row['id']) {
$goOn = 0;
break;
}
}
if ($goOn == 1) {
$tree .= $nav_row['name'] . "<br>";
array_push($exclude, $nav_row['id']);
if ($nav_row['id'] < 6) {
$top_level_on = $nav_row['id'];
}
$tree .= build_child($nav_row['id']);
}
}
function build_child($oldID) {
global $exclude, $depth;
$child_query = mysql_query("SELECT * FROM `categories` WHERE parent_id=" . $oldID);
while ($child = mysql_fetch_array($child_query)) {
if ($child['id'] != $child['parent_id']) {
for ($c=0; $c < $depth; $c++) {
$tempTree .= " ";
}
$tempTree .= "- " . $child['name'] . "<br>";
$depth++;
$tempTree .= build_child($child['id']);
$depth--;
array_push($exclude, $child['id']);
}
}
return $tempTree;
}
echo $tree;
?>
它依赖于下面的MySQL数据库结构:
id | parent_id | name
1 Cats
2 1 Siamese Cats
3 2 Lilac Point Siamese Cats
4 Dogs
etc...
脚本允许无限制的类别深度,但有一个主要的垮台。它显示了类别导航到前端像这样:
Cats
- Siamese Cats
- Lilac Point Siamese Cats
Dogs
我怎样才能把它显示是这样的:
Cats
- Siamese Cats
- Lilac Point Siamese Cats
Dogs
让每增加品类深度被添加到开始另一个空间类别文本的缩进?
随着一些相当整洁的查询,你可以让你的SQL做大部分的工作:http://dev.mysql.com/tech-resources/articles/hierarchical-data.html – 2010-06-19 17:57:23