Hi,
time to use recursion ...
You either need to get the data sorted by the query or by a custom function, since it doesn't work otherwise.
The looks a little bit complex but my tests were successfull:
<?PHP
// insert code to connect to db server and to select database here
// ....
//$sql = "SELECT Country,Sort,Type,id FROM example ORDER BY Country,Sort,Type,id";
//$res = mysql_query($sql,$dbconnect) or die ("SQL error: ".mysql_error());
$arr = array();
// test array
$arr[] = array('Country' => 'Spain','Node2' => 'Zing','Dummy' => 'Test','Sort' => 'Fruit','Type' => 'Orange','id' => 1);
$arr[] = array('Country' => 'Spain','Node2' => 'Zing','Dummy' => 'Test','Sort' => 'Vegetable','Type' => 'Tomato','id' => 2);
$arr[] = array('Country' => 'Cuba','Node2' => 'Zing','Dummy' => 'Test','Sort' => 'Fruit','Type' => 'Banana','id' => 3);
$arr[] = array('Country' => 'Cuba','Node2' => 'Zing','Dummy' => 'Test','Sort' => 'Vegetable','Type' => 'Cucumber','id' => 4);
$arr[] = array('Country' => 'Italy','Node2' => 'Zing','Dummy' => 'Test','Sort' => 'Fruit','Type' => 'Apple','id' => 5);
$arr[] = array('Country' => 'Italy','Node2' => 'Zing','Dummy' => 'Test','Sort' => 'Vegetable','Type' => 'Tomato','id' => 6);
$arr[] = array('Country' => 'Italy','Node2' => 'Zong','Dummy' => 'Test2','Sort' => 'Vegetable','Type' => 'Cucumber','id' => 7);
$arr[] = array('Country' => 'Italy','Node2' => 'Zong','Dummy' => 'Test3','Sort' => 'Vegetable','Type' => 'Cucumber','id' => 9);
$arr[] = array('Country' => 'Italy','Node2' => 'Zong','Dummy' => 'Test3','Sort' => 'Vegetable','Type' => 'Tomato','id' => 10);
$tree = "<ul class=\"mktree\" id=\"tree1\">\n";
$nodeVals = array();
$treeDepth = 0;
$actualDepth = 0;
function createTree($arr) {
// get the global variables and reset retVal
global $treeDepth,$nodeVals,$actualDepth;
$retVal = "";
// get remaining array keys
$keys = array_keys($arr);
// get names of first and second key
$firstKey = $keys[0];
$secondKey = $keys[1];
if (count($arr) > 0 && is_array($arr[$firstKey])) {
// the next row
$actualDepth = 0;
// create node
$retVal .= createTree($arr[$firstKey]);
// remove first row
unset($arr[$firstKey]);
// now up to the next row
$retVal .= createTree($arr);
} else {
if (count($arr)>0 && !is_array($arr[$firstKey])) {
// within current row
// but how deep ?
$actualDepth = $treeDepth - count($arr);
if (count($arr) == 2) {
// output link
if ($nodeVals[$actualDepth] != $arr[$firstKey]) {
// a new node name
$retVal .= "<li><a href=\"page_".$arr[$secondKey].".html\">".$arr[$firstKey]."</a></li>\n";
// store name of current node
$nodeVals[$actualDepth] = $arr[$firstKey];
} else {
// just the same node
$retVal .= "<li><a href=\"page_".$arr[$secondKey].".html\">".$arr[$firstKey]."</a></li>\n";
}
} else if (count($arr) > 2) {
// we didn't reach the end of the node
if ($nodeVals[$actualDepth] != $arr[$firstKey]) {
// but we have a new node name
if ($nodeVals[$actualDepth] != "") {
if ($actualDepth == 0 ) {
// root node so we need to close all the sub nodes created before
for ($cnt=0;$cnt<($treeDepth-2);$cnt++)
$retVal .= "</ul></li>";
// now we need to reset the nodeVals array since this is a new root node
$nodeVals = array();
} else {
for ($cnt=$actualDepth+1;$cnt<$treeDepth-1;$cnt++) {
// not a root node but a node with a new name so we need to close
// the nodes down to the current
$retVal .= "</ul></li>";
}
}
for ($cnt=$actualDepth+1;$cnt<($treeDepth);$cnt++) {
// and now reset the node names below this node
$nodeVals[$cnt] = "";
}
}
// print the new node name
$retVal .= "<li>".$arr[$firstKey]."<ul>";
// update the name in the node name array
$nodeVals[$actualDepth] = $arr[$firstKey];
}
// remove that node from the current row array
unset($arr[$firstKey]);
// continue with the next node(s)
$retVal .= createTree($arr);
}
}
}
// now return the current list
return $retVal;
}
$treeDepth = count($arr[0]);
$tree .= createTree($arr);
echo $tree;
?>
Thomas