I used the following stackoverflow thread, which was very helpful, in creating a sorted multilevel parent->child array.
The below is a sample multi-level (3 levels) parent->child sorted array based on the sorting method of the above stated thread:
Array
(
[1] => Array
(
[name] => User Manual
[parentId] => 0
[children] => Array
(
[2] => Array
(
[name] => Users
[parentId] => 1
[children] => Array
(
[4] => Array
(
[name] => Privileges
[parentId] => 2
)
)
)
[3] => Array
(
[name] => Reference
[parentId] => 1
[children] => Array
(
[5] => Array
(
[name] => Glossary
[parentId] => 3
)
[6] => Array
(
[name] => Index
[parentId] => 3
)
)
)
)
)
)
Now I'd like to take that sorted array and generate a single HTML SELECT that displays and represents the parent->child structure like the below example:
<option value='1'>User Manual</option>
<option value='2'>User Manual -> Users</option>
<option value='4'>User Manual -> Users -> Privileges</option>
<option value='3'>User Manual -> Reference</option>
<option value='5'>User Manual -> Reference -> Glossary</option>
<option value='6'>User Manual -> Reference -> Index</option>
The goal as seen above is to compile a display string that represents the multi-level parent/child path for each item. The VALUE to each option is the last item in the represented compiled string. So for the OPTION 'User Manual -> Users -> Privileges' the VALUE is the ID for Privileges, but the display text represents it's parent/child relationship in the array.
The select I create always ends up displaying the list of options like the below:
<option value='1'>User Manual</option>
<option value='2'>User Manual -> Users</option>
<option value='4'>Users -> Privileges</option>
<option value='3'>User Manual -> Reference</option>
<option value='5'>Reference -> Glossary</option>
<option value='6'>Reference -> Index</option>
You can see that it only is able to keep track of one level of the parent/child relationship.
My function that processes the array to create the selects options list is as follows:
function buildOptions($arr, $target, $parent = NULL) {
$html = "";
foreach ( $arr as $key => $v )
{
if (array_key_exists('children', $v))
{
if ( $key == $target )
$html .= "<option value='$key' selected>$parent {$v['name']}</option>\n";
else
$html .= "<option value='$key'>$parent {$v['name']}</option>\n";
$html .= buildOptions($v['children'],$target,$v['name']." > ");
}
else if ( $key == $target )
$html .= "<option value='$key' selected>$parent {$v['name']}</option>\n";
else
$html .= "<option value='$key'>$parent {$v['name']}</option>\n";
}
return $html;
}
I'm having trouble in keeping track of the previous X parent.name when there is more than one level. Any help here would be welcomed. Thank you!
toUL
? What's$target
? Would you consider using visual cues (indentation) instead of repeating the complete path for expressing hierarchy?int
down the recursion stack, replace$parent = null
with$level = 0
and callbuildOptions(<...>, $level + 1)
and then use$level
as a margin/padding multiplier.]