|
Posted by Zilla on 11/11/59 11:16
> How can I display this hierarchy in simple nested <li> tags in the most
> efficient way possible?
Now I have had the time to look at your problem. Here is what I came up
with. Remember that the CEO(s) should report to themselves or else it
wont work (look at id 1 and 2 which are CEOs).
I used this array to test with:
$employees = array(1 => array("name1", "title1", 1, "date1"),
2 => array("name2", "title2", 2, "date2"),
33 => array("name3", "title3", 15, "date3"),
15 => array("name4", "title4", 10, "date4"),
10 => array("name5", "title5", 1, "date5"),
37 => array("name6", "title6", 15, "date6"),
40 => array("name7", "title7", 33, "date7"),
45 => array("name8", "title8", 10, "date8"),
50 => array("name9", "title9", 37, "date9"),
4 => array("name10", "title10", 2, "date10"),
7 => array("name11", "title11", 23, "date11"),
456 => array("name12", "title12", 50, "date12"),
89 => array("name13", "title13", 4, "date13"),
23 => array("name14", "title14", 25, "date14"),
57 => array("name15", "title15", 45, "date15"),
25 => array("name16", "title16", 4, "date16")
);
And here's the code:
<?php
//builds an array of relations in format the id1_id2 where
//id2 reports to id1
foreach($employees as $key => $value) {
if($value[2] != $key) {
$relations[] = $value[2] . "_" . $key;
}
}
//sort the array in a natural order
natsort($relations);
//this function builds an array with flat tree structures
//in the format id1_id2_id3_id4_....idx
function make_tree_structure($array) {
$array2 = $array;
$i = 0;
while(list($key, $value) = each($array)) {
$explode = explode("_", $value);
$n = count($explode);
$n = $n - 1;
$under = $explode[$n];
while(list($key2, $value2) = each($array2)) {
if($key != $key2) {
$explode2 = explode("_", $value2);
$n2 = count($explode2) - 1;
$under2 = "";
while($n2 > 0) {
$under2 = $explode2[$n2] . "_" . $under2;
$n2--;
}
$under2 = substr($under2, 0, -1);
$over2 = $explode2[0];
if($over2 == $under) {
$newarray[] = $value . "_" . $under2;
$i = 1;
$x = 1;
unset($array[$key2]);
unset($array2[$key2]);
}
}
}
if($i == 1) {
unset($array[$key]);
unset($array2[$key]);
$i = 0;
}
reset($array2);
}
foreach($array as $value) {
$newarray[] = $value;
}
natsort($newarray);
if($x == 1) {
$newarray = make_tree_structure($newarray);
} else {
$newarray = $array;
}
return $newarray;
}
$flat_tree_structure = make_tree_structure($relations);
//builds a multidimensional array from the flat structures
function build_multidimensional_array($array) {
foreach($array as $value) {
$explode = explode("_", $value);
$string = "";
foreach($explode as $value2) {
$string = $string . "[" . $value2 . "]";
}
$string = "\$newarray" . $string . " = 0;";
eval($string);
}
return $newarray;
}
$tree_structure = build_multidimensional_array($flat_tree_structure);
//loops over the multidimensional array and outputs nested lists
function output_nested_lists($array, $employees) {
foreach($array as $key => $value) {
echo "<ul>\n";
echo "<li>" . $employees[$key][1] . "</li>\n"; //You
could change this...
if(is_array($value)) {
output_nested_lists($value, $employees);
}
echo "</ul>\n";
}
}
output_nested_lists($tree_structure, $employees);
?>
Outputs this with the example employees array:
* title1
o title5
+ title4
# title3
* title7
# title6
* title9
o title12
+ title8
# title15
* title2
o title10
+ title16
# title14
* title11
+ title13
If you want the name to be written just use $employees[$key][0] where I
wrote "//You could change this".
Zilla.
Navigation:
[Reply to this message]
|