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>
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>
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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.