【问题标题】:Category select with parent_id and level indication带有 parent_id 和级别指示的类别选择
【发布时间】:2017-09-04 13:59:29
【问题描述】:

我有以下数组,我怎样才能进行带有级别指示的选择。

级别指示必须是 - level 1 , -- level 2, -- level 3 --- , .......

array( 0 => array(
            'id' => 1,
            'name' => 'Bikes',
            'description' => 'bikes desc',
            'parent_id' => 0
            'childs' => array(
                0 => array(
                    'id' => 2,
                    'name' => 'Mountainbike',
                    'description' => 'mountainbike desc',
                    'parent_id' => 1,
                    'childs' => array(
                        0 => array(
                            'id' => 3,
                            'name' => 'Mountainbike spikes',
                            'description' => 'Mountainbike spikes',
                            'parent_id' => 2
                        ),
                    ),
                ),
                1 => array(
                    'id' => 5,
                    'name' => 'Sportbike',
                    'description' => 'sportbike',
                    'parent_id' => 1
                ),
            ),
        ),
        1 => array(
            'id' => 4,
            'name' => 'Car',
            'description' => 'car',
            'parent_id' => 0
        ),
    );

理想的输出:

<select>
    <option id="1">Bikes</option>
    <option id="2">- Mountainbike</option>
    <option id="3">-- Mountainbike spikes</option> 
    <option id="5">- Sportbike</option> 
    <option id="4">Car</option> 
</select>

【问题讨论】:

  • 你应该看看“递归函数”。
  • @MagnusEriksson 我怎样才能做到这一点?
  • 你有没有研究过并尝试过什么? SO 不是免费的编码服务。我已经为您提供了搜索内容的起点。这是一个解释:stackoverflow.com/questions/2648968/… 现在由你来实际阅读、学习和尝试一些东西。
  • @MagnusEriksson 你说得对,但是有一个例子更好理解,发现这个并且效果很好stackoverflow.com/questions/10011194/…

标签: php


【解决方案1】:

给你。

此代码将生成最多 3 级的选择框。

<?php
$arr = array(
        0 => array(
            'id' => 1,
            'name' => 'Bikes',
            'description' => 'bikes desc',
            'parent_id' => 0,
            'childs' => array(
                0 => array(
                    'id' => 2,
                    'name' => 'Mountainbike',
                    'description' => 'mountainbike desc',
                    'parent_id' => 1,
                    'childs' => array(
                        0 => array(
                            'id' => 3,
                            'name' => 'Mountainbike spikes',
                            'description' => 'Mountainbike spikes',
                            'parent_id' => 2
                        ),
                    ),
                ),
                1 => array(
                    'id' => 5,
                    'name' => 'Sportbike',
                    'description' => 'sportbike',
                    'parent_id' => 1
                ),
            ),
        ),
        1 => array(
            'id' => 4,
            'name' => 'Car',
            'description' => 'car',
            'parent_id' => 0
        ),
    );
?>
<!DOCTYPE html>
<html>
<head>
    <title>Multi Level</title>
</head>
<body>
    <select>
<?php
    $main = $str = '';
    foreach ($arr as $levelOne) //$arr is the array as you given above, So please change it with your parameter.
    {
        $str = '<option id="'.$levelOne['id'].'"> - '.$levelOne['name']; // First option from level one array
        if(isset($levelOne['childs']) && sizeof($levelOne['childs'] > 0)) // Checking if the level has child? if yes then we again add the option along with name and id.
        {
            foreach($levelOne['childs'] as $levelTwo)
            {
                $str .= '<option id="'.$levelTwo['id'].'"> -- '.$levelTwo['name'];
                if(isset($levelTwo['childs']) && sizeof($levelTwo['childs'] > 0))
                {
                    foreach($levelTwo['childs'] as $levelThree)
                    {
                        $str .= '<option id="'.$levelThree['id'].'"> --- '.$levelThree['name'].'</option>'; // Level three option
                    }
                }
                $str .= '</option>'; // Level Two option
            }
        }
        $str .= '</option>'; // Level One option
        $main .= $str; // each time we are adding generated option list in the main string. 
    }
    echo $main;
 ?>
    </select>
</body>
</html>

【讨论】:

  • 你应该在你的答案中包含一个解释,以解释为什么 OP 应该使用它,它实际上做了什么以及它如何解决问题。您可能还想补充一点,此代码最多只支持三个级别。
【解决方案2】:

您可以将以下代码用于您的解决方案。

echo "<select name=''>";
$opt = "";
if (count($array) > 0) {
    foreach ($array as $data) {
        if ($data['parent_id'] == 0) {
            $opt .= "<option value='" . $data['id'] . "'>" . $data['name'] . "</option>";
            if (count($data['childs']) > 0) {
                foreach ($data['childs'] as $child) {
                    $opt .= "<option value='" . $child['id'] . "'>-" . $child['name'] . "</option>";
                    if (count($child['childs']) > 0) {
                        foreach ($child['childs'] as $sub_child) {
                            $opt .= "<option value='" . $sub_child['id'] . "'>--" . $sub_child['name'] . "</option>";
                        }
                    }
                }
            }
        }
    }
    echo $opt;
}
echo "</select>";

你得到如下输出。

<select name="">
<option value="1">Bikes</option>
<option value="2">-Mountainbike</option>
<option value="3">--Mountainbike spikes</option>
<option value="5">-Sportbike</option>
<option value="4">Car</option>
</select>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-27
    • 2019-02-04
    • 2019-06-20
    • 2020-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多