First we set up our PHP page by including the Nitobi XML API (nitobi.xml.php) which is included.
header('Content-type: text/xml'); require("nitobi.xml.php");
Next we set up our database and perform our query:
mysql_connect('localhost', 'root', '') or die(mysql_error());
mysql_select_db("nitobi_testdb_v1") or die(mysql_error());
N.B. The database nitobi_testdb_v1 is defined in the file NitobiSampleDatabases.sql that is located in the samples directory of your Complete UI installation.
Then we get the parameters from the request that we'll use to construct the query:
$treeId='default';
if (isset($_GET['treeId'])) {
$treeId = $_GET['treeId'];
}
$nodeId='0';
if (isset($_GET['id'])) {
$nodeId = $_GET['id'];
}
Now we execute a query on the database.
$myQuery = "SELECT * FROM " . $tableName . " WHERE RegionOwner = " . $nodeId . " ORDER BY " . $sortColumn . ";";
$result = mysql_query($myQuery) or die(mysql_error());
($tableName, RegionOwner, and $sortColumn will need to be changed depending on your database schema).
Using the returned data, we can now loop through each record and construct an XML document using the supplied Nitobi XML API. To begin, we setup the EBAGetHandler and define the fields we are returning:
$getHandler = new EBAGetHandler();
$getHandler->DefineField("id");
$getHandler->DefineField("label");
$getHandler->DefineField("nodetype");
$getHandler->DefineField("haschildren");
Now we loop through the data and return the XML to the client:
while ($row = mysql_fetch_array($result))
{
$record = new EBARecord($row["RegionID"]);
$record->add("id", $row["RegionID"]);
$record->add("label", $row["RegionName"]);
$myQuery = 'SELECT COUNT(1) AS NumChildren FROM ' . $tableName . ' WHERE RegionOwner = ' . $row["RegionID"] . ';';
$countChildren = mysql_fetch_array(mysql_query($myQuery)) or die(mysql_error());
$numChildren = $countChildren["NumChildren"];
if ($numChildren > 0) {
$record->add("nodetype", "node");
$record->add("haschildren", "true");
} else {
$record->add("nodetype", "leaf");
$record->add("haschildren", "false");
}
$getHandler->add($record);
}
$getHandler->CompleteGet();
?>