Bre*_*gan 5 html php markup loops nested
我有一些分层数据,我需要在一系列嵌套的UL中显示.对于每个项目,我有一个名称,一个ID和一个深度值.通常我会按深度对这些项进行分组,但实际上我需要使用我的数据创建树结构,如下所示:

这是我的问题:有没有一种很好的方法来生成有效的标记(如果我能用正确的标签打印出来,我会很喜欢它,但这很难)我的数据将被嵌套在UL中?我已经有一个有点工作的解决方案,但我得到一个单一的迷路标签.这是我的代码:
<?php
include("includes/classes/Database.class.php");
$db = new Database();
$query = "SELECT COUNT(parent.Name) - 2 as level, node.Name AS Name, node.ID
FROM Region AS node, Region AS parent
WHERE node.LeftVal BETWEEN parent.LeftVal AND parent.RightVal and node.Name <> 'Earth'
GROUP BY node.ID
ORDER BY node.LeftVal";
$results = $db->executeQuery($query);
?>
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
$last_level = 0;
?>
<ul id="regionTree">
<?php
while ($row = mysql_fetch_assoc($results)) {
$link = '<li>'.PHP_EOL.'<a href="addChild.php?parentid='.$row["ID"].'">'.$row["Name"]."</a>".PHP_EOL;
$diff = $last_level - $row["level"];
if($diff == 0){
// Sibling
echo ($row["level"] != 0) ? '</li>'.PHP_EOL.$link:$link;
}
elseif($diff < 0){
// Child
$demoter = '<ul>'.PHP_EOL;
for ($i=0; $i > $diff; $i--) {
echo $demoter;
}
echo $link;
}
else{
// Parent
$promoter = '</li>'.PHP_EOL.'</ul>';
for ($i=0; $i < $diff; $i++) {
echo ($row["level"] != 0) ? $promoter.PHP_EOL."</li>":$promoter;
}
echo $link;
}
$last_level = $row["level"];
}
?>
</li>
</ul>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
:: Edit ::我创建了一个带有生成源的pastebin,它不会验证. Pastebin.com
:: EDIT 2 ::这是Region表的架构.它使用嵌套集模型和邻接列表模型的混合设计.
CREATE TABLE Region (
ID INT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'Stores the ID for the Region.',
Name VARCHAR(45) NOT NULL COMMENT 'Stores the name of the Region',
Region_Type VARCHAR(45) NOT NULL COMMENT 'Stores the Region type.',
Parent INT COMMENT 'Stores the ID of the Parent Region',
LeftVal INT NOT NULL,
RightVal INT NOT NULL,
PRIMARY KEY (ID)
) COMMENT 'Stores information about all Regions.' ENGINE=INNODB
ROW_FORMAT=DEFAULT CHARACTER SET utf8 collate utf8_general_ci;
Run Code Online (Sandbox Code Playgroud)
这应该工作:
$query = "SELECT node.Name, (COUNT( parent.Name ) -1) AS depth FROM region AS node
CROSS JOIN region AS parent
WHERE node.LeftVal BETWEEN parent.LeftVal
AND parent.RightVal
GROUP BY node.Name
ORDER BY node.LeftVal";
$result = mysql_query($query);
// Build array
$tree = array();
while ($row = mysql_fetch_assoc($result)) {
$tree[] = $row;
}
// Bootstrap loop
$result = '';
$currDepth = 0;
$lastNodeIndex = count($tree) - 1;
// Start the loop
foreach ($tree as $index => $currNode) {
// Level down? (or the first)
if ($currNode['depth'] > $currDepth || $index == 0) {
$result .= '<ul>';
}
// Level up?
if ($currNode['depth'] < $currDepth) {
$result .= str_repeat('</ul></li>', $currDepth - $currNode['depth']);
}
// Always open a node
$t = ($index == 0) ? 1 : 2;
$result .= '<li>' . $currNode['Name'];
// Check if there's chidren
if ($index != $lastNodeIndex && $tree[$index + 1]['depth'] <= $tree[$index]['depth']) {
$result .= '</li>'; // If not, close the <li>
}
// Adjust current depth
$currDepth = $currNode['depth'];
// Are we finished?
if ($index == $lastNodeIndex) {
$result .= '</ul>' . str_repeat('</li></ul>', $currDepth);
}
}
// Indent the code
// For UTF8: tidy_parse_string($result, array('indent' => true, 'show-body-only' => true), 'UTF8')
$result = tidy_parse_string($result, array('indent' => true, 'show-body-only' => true));
print $result;
Run Code Online (Sandbox Code Playgroud)
如何从此结果集创建数组(使用遍历模型存储在数据库中的嵌套类别)?