【问题标题】:Indented HTML <select> dropdown with hierarchical adjacency model MySql and PHP带有分层邻接模型 MySql 和 PHP 的缩进 HTML <select> 下拉菜单
【发布时间】:2019-09-08 02:17:07
【问题描述】:

我需要一个非常常见的选择下拉菜单,它显示分层项目,其中所有子项目通过添加 &amp;nbsp; 进行缩进.当项目以正确的顺序显示时(正确的父母下的孩子),我如何使用空格技巧正确地使下拉缩进,而不是缩进?

在 Mysql 中,我有一个包含 Category_ID Item_Name 和 Parent_ID 列的表。

在 php 中,我使用查询获取一个包含 Category 和 Parent 列的自引用表

在 php 中,使用获取的类别表和直接父类别,我调用函数 parseTree();它采用 sql 结果数组,并返回完整树结构的数组。

在 php 中我调用了一个函数 printTree();在 &lt;select&gt; 标签之间递归循环遍历树数组并用 &lt;option&gt; 标签回显每个节点。

在 printTree() 中是一个函数 printChildren(),其目的是在找到时为所有子数组添加缩进。这不起作用。

期望的结果:

<select>
 <option>Root</option>
 <option>Root</option>
  <option>&nbsp;Child Level 1</option>
   <option>&nbsp;&nbsp;Child Level 2</option>
     <option>&nbsp;&nbsp;&nbsp;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 .= "&nbsp;&nbsp;";
        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 Rootchild level1child level 2 。 . .等更好地通过样本数据提供所需的输出
  • @Dharman 在此行之后HTML/PHP TO EXECUTE FUNCTIONS AND POPULATE SELECT 选择下拉框将在 php 中使用 html
  • @Dharman 我的问题不是如何在 HTML 中创建简单的静态下拉菜单,而是如何使用 PHP 动态添加不同数量的 &nbsp 以根据它们在树下的距离缩进选项(也未知)。如果你知道如何在没有 PHP 的情况下做到这一点,请告诉我如何。

标签: php mysql treeview html-select adjacency-list-model


【解决方案1】:

据我了解,如果 categoryid 是区分所有产品,那么我的回答对你有用。

没有必要再次离开同一张表。

$sql = 'select cat_ID from categories_tbl';
$result = mysqli_query($db, $sql);
echo '<select class="form-control">';
while ($row = mysqli_fetch_assoc($result)){
    $sqlcat_name  = "select cat_Name from categories_tbl where parent_ID="."'".$row['cat_ID']."'"; 
    // echo this query to check that properly formed
    $Secondresult = mysqli_query($db, $sqlcat_name);
    $num_rows     = mysql_num_rows($Secondresult);
    $Secondrow    = mysqli_fetch_assoc($Secondresult)
    for($a=0;$a<$num_rows;$a++){
        echo'<option>';
        for($b=0;$b<$a;$b++){
            echo"   ";
        }
        echo "$Secondrow[$a]"."</option>";
    }
}
echo '</select>';

【讨论】:

  • 谢谢,但它不起作用。我想我理解你的想法,为每一行运行 sql 以获取子类别。拜托,我认为您的答案中可能存在一些错误,我无法完全理解正在发生的事情。这段代码也只有两个级别吗?
  • 我得到的第一个错误是未定义的偏移量 0、1 等,用于 echo "$Secondrow[$a]"."";;
  • 我认为有几处可能是错误的,或者我不明白如何使用此代码。每个类别只能有一个 parent_ID,但每个 parent_ID 可以有多个孩子。请看看你是否可以检查你的代码。谢谢。
【解决方案2】:

对我有用的代码,我上面的问题最终如下。我只需要计算调用递归函数来填充 select 的次数,每次函数进入新级别的类别时,我都可以根据需要使用计数值重复一个 '&nbsp' 字符串,为递归的每个级别。

这将正确对齐选择中每个类别中的节点,因此用户可以看到哪些子类别属于哪个父类别。

我为此使用了一个静态变量,但打算用更好的方法替换它。

我将发布创建选择下拉列表的部分代码,以防任何人发现它有用。


             function printTree($tree) {


                        function printChildren($childrenarray) {
                               static $level = 1;
                            if(!is_null($childrenarray) && count($childrenarray) > 0){
                               $level++; 
                                        foreach($childrenarray as $node) {

                                            echo '<option>'.str_repeat('&nbsp;', $level).$node['name'].'</option>';

                                                printChildren($node['children']);


                                        }

                                --$level;

                            }
                        }

                    if(!is_null($tree) && count($tree) > 0){

                        foreach($tree as $node) {
                            echo '<option bg-danger>'.$node['name'].'</option>';

                                if (!is_null($node['children']) && count($node['children']) > 0 ) {

                                    printChildren($node['children']);
                                }

                        }
                    }

             }


【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多