带有分层邻接模型MySql和PHP的缩进HTML下拉列表

问题描述 投票:0回答:2

我需要一个非常常见的选择下拉列表,显示分层项目,其中所有子项目通过添加 缩进我已经得到了用正确的分层顺序填充php中的下拉列表,但不能让它缩进。当项目以正确的顺序显示(右父母下的孩子)时,如何使用空间技巧正确地进行下拉缩进,而不是缩进?

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

在php中,使用查询我获取一个具有Column和Parent列的自引用表

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

在php中我调用了一个函数printTree();在<select>标签之间递归循环遍历树阵列,并用<option>标签回显每个节点。

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] => 
                                        )

                                )

                        )

                )

        )

)

php mysql treeview html-select adjacency-list-model
2个回答
0
投票

据我了解,如果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>';

0
投票

对我有用的代码,我上面的问题最终如下。我只需要计算我的递归函数填充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']);
                                }

                        }
                    }

             }


© www.soinside.com 2019 - 2024. All rights reserved.