【发布时间】:2019-09-08 02:17:07
【问题描述】:
我需要一个非常常见的选择下拉菜单,它显示分层项目,其中所有子项目通过添加   进行缩进.当项目以正确的顺序显示时(正确的父母下的孩子),我如何使用空格技巧正确地使下拉缩进,而不是缩进?
在 Mysql 中,我有一个包含 Category_ID Item_Name 和 Parent_ID 列的表。
在 php 中,我使用查询获取一个包含 Category 和 Parent 列的自引用表
在 php 中,使用获取的类别表和直接父类别,我调用函数 parseTree();它采用 sql 结果数组,并返回完整树结构的数组。
在 php 中我调用了一个函数 printTree();在 <select> 标签之间递归循环遍历树数组并用 <option> 标签回显每个节点。
在 printTree() 中是一个函数 printChildren(),其目的是在找到时为所有子数组添加缩进。这不起作用。
期望的结果:
<select>
<option>Root</option>
<option>Root</option>
<option> Child Level 1</option>
<option> Child Level 2</option>
<option> Child Level 3</option>
</select>
等等……
PHP
<?php
$sql = "SELECT
e.cat_Name AS 'Category',
m.cat_Name AS 'Parent Category'
FROM
categories_tbl e
lEFT JOIN
categories_tbl m ON m.cat_ID = e.parent_ID";
$result = mysqli_query($db, $sql);
function parseTree($tree, $root = "")
{
$return = array();
# Traverse the tree and search for direct children of the root
foreach($tree as $child => $parent) {
# A direct child is found
if($parent == $root) {
# Remove item from tree (we don't need to traverse this again)
unset($tree[$child]);
# Append the child into result array and parse its children
$return[] = array(
'name' => $child,
'children' => parseTree($tree, $child)
);
}
}
return empty($return) ? NULL : $return;
}
function printTree($tree)
{
$indent = "";
function printChildren($childrenarray)
{
$indent .= " ";
if(!is_null($childrenarray) && count($childrenarray) > 0) {
foreach($childrenarray as $node) {
echo '<option>' . $indent . $node['name'] . '</option>';
printChildren($node['children']);
}
}
}
if(!is_null($tree) && count($tree) > 0) {
foreach($tree as $node) {
echo '<option>' . $indentpaddingval . $node['name'] . '</option>';
if(!is_null($node['children']) && count($node['children']) > 0) {
printChildren($node['children']);
}
}
}
}
?>
HTML/PHP 执行函数并填充选择
<select class="form-control">
<?php
$cat_tree_arr = array();
while ($row = mysqli_fetch_assoc($result)) {
$cat_tree_arr[$row['Category']] = $row['Parent Category'];
}
$result = parseTree($cat_tree_arr);
printTree($result);
?>
</select>
我在此处添加包含父/子数组中的整个类别树的已解析数组:
Array
(
[0] => Array
(
[name] => All Raw Materials
[children] => Array
(
[0] => Array
(
[name] => Bag Raw Materials
[children] => Array
(
[0] => Array
(
[name] => LDPE Materials
[children] =>
)
)
)
)
)
[1] => Array
(
[name] => All Finished Goods
[children] => Array
(
[0] => Array
(
[name] => Local Finished Goods
[children] => Array
(
[0] => Array
(
[name] => Local Bags
[children] =>
)
)
)
[1] => Array
(
[name] => Export Finished Goods
[children] => Array
(
[0] => Array
(
[name] => Export Bags
[children] =>
)
)
)
)
)
)
【问题讨论】:
-
如果你显示这个
$row = mysqli_fetch_assoc($result)的数据就可以了 -
@Dharman 但他正在构建 php 中的选择下拉菜单
-
很难想象
Root Root和child level1和child level 2。 . .等更好地通过样本数据提供所需的输出 -
@Dharman 在此行之后
HTML/PHP TO EXECUTE FUNCTIONS AND POPULATE SELECT选择下拉框将在 php 中使用 html -
@Dharman 我的问题不是如何在 HTML 中创建简单的静态下拉菜单,而是如何使用 PHP 动态添加不同数量的   以根据它们在树下的距离缩进选项(也未知)。如果你知道如何在没有 PHP 的情况下做到这一点,请告诉我如何。
标签: php mysql treeview html-select adjacency-list-model