I'm just fooling around with an idea and wanted to try building a "tree" from a simple flat file like so:
11 RUM_TOP
12 RUM_TOP
13 RUM_CHILD 11
14 RUM_CHILD 13
15 RUM_CHILD 12
16 RUM_CHILD 15
17 RUM_CHILD 16
18 RUM_TOP
19 RUM_CHILD 18
10 RUM_CHILD 19
(Why? I'm thinking about creating a forum script, and you have to have a tree to display a threaded forum...)
Anyway, I tried writing a simple trial script to take the file above and spit out a simple HTML tree (using the ul tag). I would wait until I had more of my forum's other modules in place before adding things like thread sorting and whatnot, not to mention converting the numbers to pages (another module altogether probably).
I tried to borrow a page from LUA scripting (which I've been playing with a bit), and use a $array["n"] (which LUA would allow me to write $array.n as well) to tell how many nodes are in that array (and then each sub node could have a ["n"] and further sub nodes...). I'm pretty happy with what I came up with, using recursion to "walk"/"crawl" completely through the tree. However, I get about 50 errors about how I can't use a string index on line 30. Does anyone know what I am doing wrong? I thought PHP allowed for string indexes...
Here's what I created:
<?php
/*****************************************************************
Treehouse Test... builds a treehouse from a flat file. Spits
out a HTML tree.
*****************************************************************/
function find_node($node_name, $tree) {
for ($i = 0; $i <= $tree["n"]; $i++) {
if ($tree[$i]["n"] > -1) {
$recurse = find_node($node_name, $tree[$i]);
if ($recurse["n"] > -1) {
$recurse["n"]++;
$recurse[$recurse["n"]] = $i;
return $retval;
}
}
if ($tree[$i] == $node_name) {
$retval["n"] = 0;
$retval[0] = $i;
return $retval;
}
}
$retval["n"] = -1;
return $retval;
}
function set_node($node_name, &$tree, $newval) {
for ($i = 0; $i <= $tree["n"]; $i++) {
if ($tree[$i]["n"] > -1) {
set_node($node_name, $tree[$i], $newval);
}
if ($tree[$i] == $node_name) {
$tree[$i]["n"]++;
$tree[$i][$tree[$i]["n"]] = $newval;
$tree[$i][$tree[$i]["n"]]["n"] = -1;
}
}
}
function tree_crawl($tree) {
echo "<ul>";
for ($i = 0; $i <= $tree["n"]; $i++) {
echo "<li>$tree[$i]";
if ($tree[$i]["n"] > -1) {
tree_crawl($tree[$i]);
}
}
echo "</ul>";
}
$fp = fopen("treehouse.dat", "r");
$treecontents = fread($fp, filesize("treehouse.dat"));
$treepeices = explode("\n", $treecontents);
$treehouse["n"] = -1;
for ($i = 0; $i <= count($treepeices); $i++) {
$treebob = explode(" ", $treepeices[$i]);
if ($treebob[1] == "RUM_TOP") {
$treehouse["n"]++;
$treehouse[$treehouse["n"]] = $treebob[0];
$treehouse[$treehouse["n"]]["n"] = -1;
} else if ($treebob[1] == "RUM_CHILD") {
These would have worked had I PHP4...
//$nodenum = find_node($treebob[2], $treehouse);
//if ($nodenum["n"] > -1) {
// $tree = &$treehouse;
// for ($i = 0; $i < $nodenum["n"]; $i++) {
// $tree = &$treehouse[$nodenum[$i]];
// }
// $tree[$nodenum[$nodenum["n"]]] = $treebob[0];
// $tree[$nodenum[$nodenum["n"]]]["n"] = -1;
//}
set_node($treebob[2], $treehouse, $treebob[0]);
} else {
echo "Error in line $i.";
}
}
tree_crawl($treehouse);
if (!fclose($fp))
echo "Error closing file.";
?>