将修改后的先序树遍历模型(嵌套集)放入中
我正在尝试将使用树遍历模型分层设置的数据转换为 << ul>为了在我的网站上显示。
这是我的代码:
function getCats($) {
// retrieve all children of $parent
$query = "SELECT max(rght) as max from t_categories";
$row = C_DB::fetchSingleRow($query);
$max = $row["max"];
$result ="<ul>";
$query = "SELECT * from t_categories where lft >=0 and rght <= $max";
if($rs = C_DB::fetchRecordset($query)){
$p_right ="";
$p_left ="";
$p_diff="";
while($row = C_DB::fetchRow($rs)){
$diff = $row["rght"] -$row["lft"];
if($diff == $p_diff){
$result.= "<li>".$row['title']."</li>";
}elseif (($row["rght"] - $row["lft"] > 1) && ($row["rght"] > $p_right)){
$result. "<ul>";
$result.= "<li>".$row['title']."</li>";
}else{
$result.= "<li>".$row['title']."</li>";
}
$p_right = $row["rght"];
$p_left = $row["lft"];
$p_diff = $diff;
}
}
$result.= "</ul>";
return $result;
}
这是我的示例表:
|ID | TITLE | lft| rght |
|1 | Cat 1 | 1 | 16 |
|18 | Cat 2 | 3 | 4 |
|22 | Cat 3 | 5 | 6 |
|28 | Cat 4 | 7 | 8 |
|34 | Cat 5 | 9 | 9 |
|46 | Cat 6 | 11 | 10 |
|47 | Cat 7 | 13 | 12 |
|49 | Cat 8 | 15 | 14 |
现在它输出类似的内容:
<ul>
<li>Cat 1</li>
<li>Cat 2</li>
<li>Cat 3</li>
<li>Cat 4</li>
<li>Cat 5</li>
<li>Cat 6</li>
<li>Cat 7</li>
<li>Cat 8</li>
</ul>
谁能告诉我为什么或如何以分层结构输出列表?
I am trying to get my data which is hierarchically set up with a tree traversal model into an < ul> in order to show on my site.
Here is my code:
function getCats($) {
// retrieve all children of $parent
$query = "SELECT max(rght) as max from t_categories";
$row = C_DB::fetchSingleRow($query);
$max = $row["max"];
$result ="<ul>";
$query = "SELECT * from t_categories where lft >=0 and rght <= $max";
if($rs = C_DB::fetchRecordset($query)){
$p_right ="";
$p_left ="";
$p_diff="";
while($row = C_DB::fetchRow($rs)){
$diff = $row["rght"] -$row["lft"];
if($diff == $p_diff){
$result.= "<li>".$row['title']."</li>";
}elseif (($row["rght"] - $row["lft"] > 1) && ($row["rght"] > $p_right)){
$result. "<ul>";
$result.= "<li>".$row['title']."</li>";
}else{
$result.= "<li>".$row['title']."</li>";
}
$p_right = $row["rght"];
$p_left = $row["lft"];
$p_diff = $diff;
}
}
$result.= "</ul>";
return $result;
}
Here is my sample table:
|ID | TITLE | lft| rght |
|1 | Cat 1 | 1 | 16 |
|18 | Cat 2 | 3 | 4 |
|22 | Cat 3 | 5 | 6 |
|28 | Cat 4 | 7 | 8 |
|34 | Cat 5 | 9 | 9 |
|46 | Cat 6 | 11 | 10 |
|47 | Cat 7 | 13 | 12 |
|49 | Cat 8 | 15 | 14 |
Now it outputs something like:
<ul>
<li>Cat 1</li>
<li>Cat 2</li>
<li>Cat 3</li>
<li>Cat 4</li>
<li>Cat 5</li>
<li>Cat 6</li>
<li>Cat 7</li>
<li>Cat 8</li>
</ul>
Can anyone tell me why or how it will output the list in hierarchical a structure?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
好的,让我们来进行一些赏金狩猎;)
第 0 步 - 清理示例:
如前所述,您的示例数据已损坏,因为它没有定义有效的嵌套集。如果您从应用程序获取此数据,则应该检查插入/删除逻辑。
因此,为了进行测试,我使用了如下所示的净化版本:
(这里是 MySQL,因为它是第一个)
第 1 步 - 让数据库进行排序
嵌套集最初是作为在数据库中存储树的便捷方式而发明的,因为它们使得查询子树、父关系以及在这种情况下特别有趣的顺序和深度变得非常容易:
这将返回您的集合整齐排序,开始与根节点并按前序继续到末尾。最重要的是,它会将每个节点的深度添加为正整数,表示该节点低于根(0 级)的级别。对于上面的示例数据,结果将是:
在代码中:
生成的数组将如下所示:
步骤 2 - 输出为 HTML 列表片段:
使用 while 循环:
与递归函数的逻辑相同:
两者都会输出以下结构:
Nitpickerscorner:发问者明确要求
,但是有序无序列表!?来吧...
;-)
Ok, let's do some bounty hunting ;)
Step 0 - Sanitize example:
As already mentioned, your example data is broken, as it does not define a valid nested set. If you took this data from an app, you should check the insert/delete logic.
So for testing, I used a sanitized version like so:
(MySQL here, as it was the first at hand)
Step 1 - Let the database do the ordering
Nested sets where primarily invented as a convenient way of storing trees in databases, as they make it pretty easy to query for subtrees, parent relations and, especially interesting in this case, for order and depth:
This will return your set neatly ordered, starting with the root node and continuing to the end in preorder. Most importantly, it will add the depth of each node as a positive integer, indicating how many levels the node is below root (level 0). For the above example data, the result will be:
In code:
The resulting array will look like this:
Step 2 - Output as HTML list fragment:
Using while loop:
Same logic as recursive function:
Both will output the following structure:
Nitpickers corner: Questioner explicitly asked for
<ul>
, but ordered unordered lists!? Come on...;-)
更好的渲染树功能对我有用(php 函数准备在 jsTree jQuery 插件中使用的 html 源)而不是 Henrik Opel 的功能:
结果 HTML:
Better Render Tree Function that worked for me (php function to prepare html source for use in jsTree jQuery plugin) instead of the Henrik Opel's one:
Result HTML:
有一个用于处理嵌套集的 PEAR 包:DB_NestedSet。
您可能还对管理 MySQL 中的分层数据一文感兴趣。
There's a PEAR package for dealing with nested sets: DB_NestedSet.
You might also be interested in the article Managing Hierarchical Data in MySQL.
这应该是您正在寻找的:
要获取嵌套树的 HTML,您应该执行以下操作:
请注意,您的嵌套集示例看起来不正确,您还应该确保我在调用您的代码时是否没有犯任何错误C_DB类,我不知道,因为我不熟悉它。
This should be what you're looking for:
To get the HTML for your nested tree you should do:
Please note that your nested set sample doesn't look right, also you should make sure if I didn't made any mistake invoking your C_DB class, I don't know since I'm not familiarized with it.
只需循环遍历结果即可:
Simply loop thru the result will do:
不同之处在于您可以渲染 X 棵树,这适用于我的一个项目。当我从数据库中获取行时,我使用 char 作为深度引用
the difference in this is that you can render X numbers of trees, this applies to one of my projects. and I use a char as a depth reference when I fetch the rows from the DB
我正在使用 CROSS JOIN 查询显示 jsTree jQuery 菜单;一切都很好!
我在现有的表格中添加了一个职位列。但是,当我定义位置并按位置对所有项目进行排序时,相应的项目未正确分组。我猜这是一个查询问题,尝试了多种组合,但没有成功。
i`m using CROSS JOIN query displaying jsTree jQuery menu; Everything works just great !
The existing table I added a column for the position. However, when I define position and ordered all by position, the corresponding items are not grouped properly. I guess it's a query issue, tried many combinations, but no success.