0

I have the following array, how can i make a select with level indication.

The level indication must be - 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
        ),
    );

Desirable output:

<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>
Bas
  • 2,330
  • 4
  • 29
  • 68
  • You should look into "recursive functions". – M. Eriksson Sep 04 '17 at 05:49
  • @MagnusEriksson how can i achieve that? – Bas Sep 04 '17 at 05:56
  • Did you look into it and tried something? SO isn't a free coding service. I've given you a starting point of what to search for. Here's an explanation: https://stackoverflow.com/questions/2648968/what-is-a-recursive-function-in-php Now it's up to you to actually read, learn and try something. – M. Eriksson Sep 04 '17 at 05:57
  • @MagnusEriksson you wright , but with an example its better to understand, found this and works great https://stackoverflow.com/questions/10011194/how-to-recursively-build-a-select-with-unknown-tree-depth – Bas Sep 05 '17 at 05:06

2 Answers2

0

Here you go.

This code will generate select box upto 3 levels only.

<?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>
Sahil Patel
  • 1,570
  • 2
  • 15
  • 34
  • You should include an explanation in your answer to explain why the OP should use this, what it actually does and how it solves the issue. You might also want to add that this code only supports up to three levels. – M. Eriksson Sep 04 '17 at 05:48
0

You can use below code for your solution.

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>";

You get output like below.

<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>
Jalpa
  • 697
  • 3
  • 13