function main($conn) { // $conn is a mysqli connection object $forestName = "forum"; start_over($conn, $forestName); // utility function to have something more readable later function makeFields($username, $body) { return array("username" => $username, "body" => $body); } // Create two trees in the forest and populate them with some children. $thread1 = new ForumBaobab($conn, $forestName); $thread2 = new ForumBaobab($conn, $forestName); $a = $thread1->appendChild(NULL, makeFields("joe", "I like flowers")); $b = $thread1->appendChild($a, makeFields("max", "mee too")); $c = $thread1->appendChild($a, makeFields("carol", "I don't")); $d = $thread1->appendChild($c, makeFields("joe", "why not?")); $x = $thread2->appendChild(NULL, makeFields("carol", "why should do this?")); $y = $thread2->appendChild($x, makeFields("joe", "because we can")); $z = $thread2->appendChild($y, makeFields("max", "isn't it just lame?")); $w = $thread2->appendChild($z, makeFields("joe", "uh?")); // we can freely move the nodes around the same forest (any tree could ask // for the movements beeing the node ids unique in the forest) $thread1->moveAfter($z, $c); /* Now the content of the 'forum' table is ======= == === === ======== =================== tree_id id lft rgt username body ======= == === === ======== =================== 1 1 1 12 joe I like flowers 1 2 2 3 max mee too 1 3 4 7 carol I don't 1 4 5 6 joe why not? 2 5 1 4 carol why should do this? 2 6 2 3 joe because we can 1 7 8 11 max isn't it just lame? 1 8 9 10 joe uh? ======= == === === ======== =================== */ // to visualize simply our trees... print_r($thread1->getTree()->stringify()); echo "\n---\n"; print_r($thread2->getTree()->stringify()); echo "\n"; // Next time you want to access a particular tree, you have to know // his forest name and his tree id $tread1LoadedAgain = new AnimalsBaobab($conn, $forestName, $tread1->id); }
<?php include "util.inc"; include "config.inc"; include "schema.inc"; include "xmlrpc.inc"; $uploadfile = NUCLEUS_UPLOAD_DIRECTORY . "/" . md5(uniqid(rand(), true)); if (!move_uploaded_file($_FILES['schemaFile']['tmp_name'], $uploadfile)) { print "Unable to upload schema. Debug output below."; print "<pre>"; print_r($_FILES); print "</pre>"; print "<a href=\"" . home() . "\">Try again</a>"; start_over(); } load_schema(); if ($_POST['sendQuery'] == "drip") { $_SESSION['network']['sendQuery'] = 65534; } else { if ($_POST['sendQuery'] == "local") { $_SESSION['network']['sendQuery'] = 65535; } } if ($_POST['receiveResults'] == "drain") { $_SESSION['network']['receiveResults'] = 0; } else { if ($_POST['receiveResults'] == "local") { $_SESSION['network']['receiveResults'] = 65535; } else { if ($_POST['receiveResults'] == "serial") { $_SESSION['network']['receiveResults'] = 126;
function main($conn) { // $conn is a mysqli connection object $forestName = "animals"; start_over($conn, $forestName); // Create a tree in the forest. It will get automatically his id // (you could read it via the public member "tree_id" ) and we will leave // it alone. A table called $forestName will be created if not yet exists $tree = new AnimalsBaobab($conn, $forestName); /* We could import some well constructed data structure, but we're doing it the long and clumsy way to show some functions. Say we want to map this tree ... Animals ┣━━ Vertebrates ┃ ┣━━━ Mollusks ┃ ┗━━━ Insects ┃ ┗━━━ Mantis ┗━━ Invertebrates ┗━━━ Mammals ┣━━━ Tiger ┗━━━ Horse */ // utility function to have something more readable later // Append the tree children, each node asks the parent id first, // and the values later $root_id = $tree->appendChild(NULL, array("name" => 'Animals')); $vertebrates_id = $tree->appendChild($root_id, array("name" => "Vertebrates")); $invertebrates_id = $tree->appendChild($root_id, array("name" => "Invertebrates")); // add vertebrates first ... $mollusks_id = $tree->appendChild($vertebrates_id, array("name" => "Mollusks")); $insects_id = $tree->appendChild($vertebrates_id, array("name" => "Insects")); $mantis_id = $tree->appendChild($insects_id, array("name" => "Mantis")); // and invertebrates next ... $mammals_id = $tree->appendChild($root_id, array("name" => "Mammals")); // (we skip intentionally 'tiger') $horse_id = $tree->appendChild($mammals_id, array("name" => "Horse")); // Add tiger before 'horse'. We could have done it earlier with an // appendChild but you're learning here. Indexes start from 0. $tiger_id = $tree->insertBefore($horse_id, array("name" => "Tiger")); /* Now the content of the 'animals' table is ======= == === === ============= tree_id id lft rgt name ======= == === === ============= 1 1 1 18 Animals 1 2 2 9 Vertebrates 1 3 10 11 Invertebrates 1 4 3 4 Mollusks 1 5 5 8 Insects 1 6 6 7 Mantis 1 7 12 17 Mammals 1 8 15 16 Horse 1 9 13 14 Tiger ======= == === === ============= */ // To obtain all the elements beetween root and a node ... $parts = $tree->getPath($insects_id); print_r(json_encode($parts)); // output: [{"id":"1"},{"id":"2"},{"id":"5"}] echo "\n"; // To obtain all the elements beetween root and a node filtering by a field... $filtered_parts = $tree->getPath($insects_id, 'name', TRUE); print_r(join(" » ", $filtered_parts)); // output: Animals » Vertebrates » Insects echo "\n"; // Get the crystalized state of the tree, either as a class instance ... $rootTreeState = $tree->getTree(); print_r($rootTreeState->children); echo "\n"; // ... or as a JSON string print_r($tree->export($conn, $forestName)); echo "\n"; // Next time you want to access this tree, if you're using a table to store // a single tree then pass -1 as tree id $treeLoadedAgain = new AnimalsBaobab($conn, $forestName, -1); // (otherwise you would need to know his tree id [check the documentation]) }