问题
Hello everybody i need to create a Dynamic Tree view in HTML and PHP from the result of a web service I have googled around the net but I'm unable to make it work... Below is my code that explodes an HTML string to be processed and putted into an array to manipulate it better and build up the tree. The idea was to follow this tutorial here: How To Create Dynamic Tree View Menu Thanks in advance.
<?php
$hTwo= $dom->getElementsByTagName('div'); // here I get my desired HTML <div> tags to use their values
function createTreeView($array, $currentParent, $currLevel = 0, $prevLevel = -1) {
foreach ($array as $category) {
if ($currentParent == $category['padre']) {
if ($currLevel > $prevLevel) echo " <ol class='tree'> ";
if ($currLevel == $prevLevel) echo " </li> ";
echo '<li> <label for="subfolder2">'.$category['desc'].'</label> <input type="checkbox" name="subfolder2"/>';
if ($currLevel > $prevLevel) { $prevLevel = $currLevel; }
$currLevel++;
createTreeView ($array, $categoryId, $currLevel, $prevLevel);
$currLevel--;
}
}
if ($currLevel == $prevLevel) echo " </li> </ol> ";
}
$trenino = array();
foreach ($hTwo as $k => $html_val) {
if ($k < 2) continue;
//echo $html_val->nodeValue;
$new_string = explode('|',$html_val->nodeValue);
$padre = $new_string[0]; //echo '<p>padre: '.$padre.'</p>';
$figlio = $new_string[1]; //echo '<p>figlio: '.$figlio.'</p>';
$desc = $new_string[2]; //echo '<p>desc: '.$desc.'</p>';
$colore = $new_string[3]; //echo '<p>colore: '.$colore.'</p>';
$lt = $new_string[4]; //echo '<p>lt: '.$lt.'</p>';
$pallino = $new_string[5];
$magazz = $new_string[6]; //echo 'magazzino: '.$magazz;
$trenino['padre'][] = $new_string[0];
$trenino['figlio'][] = $new_string[1];
$trenino['desc'][] = $new_string[2];
$trenino['colore'][] = $new_string[3];
$trenino['lt'][] = $new_string[4];
$trenino['pallino'][] = $new_string[5];
$trenino['magazz'][] = $new_string[6];
}
}
//echo '<pre>',print_r($trenino),'</pre>';
createTreeView($trenino, 0);
?>
It doesnt work properly and returns this error: ( ! ) Notice: Undefined index: padre in C:\wamp64\www\ws_leo.php on line 98
来源:https://stackoverflow.com/questions/60958188/dynamic-tree-view-with-php