DOAP - Remote Abstract Data Access Protocol by Dave

<< ParametersContentsErrors >>

Arrays

DOAP can return array data as well as standard dynamic data types. Arrays can be multi-dimensional and are index based rather than associative to fit into standard JavaScript functionality (it is planned to consider allowing use of iArray Associative JavaScript Arrays at some later date).

When a function on the DOAP Server returns an array this is detected automatically.

This is an example of working with arrays (click here to see it run).

Server-Side DOAP Server PHP
<?php
ob_start(); // ensure no messy output from includes
 
// Our Addition Function - takes two numbers and adds them returning an array
function addition($one, $two)
{
	// sanitise user input just in case...
	if (!is_numeric($one) || !is_numeric($two)) return array("Non-numeric input");
 
	$result=array();
	$result[]="First Value: ".$one;
	$result[]="Second Value: ".$two;
	$result[]="Addition Result: ".($one+$two);
 
	return $result;
}
 
function multidim() // demonstrate a multi-dimensional array
{
	$result=array( "one", "two", "three", array("A.one", "A.two"), "four");
	return $result;
}
 
require("xmlcreate.inc.php"); // include the xmlCreate library
require("doap.inc.php"); // include the DOAP Server library
 
$doap = new DOAP_Server(); // create a DOAP Server object
 
// register the addition function with a minimum and maximum of 2 parameters
// e.g. there must be two parameters
$doap->Register("addition",2,2);
 
// register the multidim function with 0 parameters
$doap->Register("multidim",0,0);
 
ob_clean(); // clean any messy output from includes
 
$doap->Process(); // get DOAP Server to process the request
?>

The server code defines two functions which both return arrays. The addition function acts the same as the previous example but returns an array of strings showing the values and sum rather than just a numeric sum. The multidim function returns a multi-dimensional array to demonstate that capability.

Client-Side DOAP Server HTML/JavaScript
<HTML>
<HEAD>
<TITLE>DOAP Example 3: Arrays</TITLE>
<!-- Include the DOAP Client JavaScript -->
<SCRIPT TYPE="text/javascript" SRC="doap.js"></SCRIPT>
 
<SCRIPT TYPE="text/javascript">
var doap = new DOAP(); // create the DOAP Client object
doap.url = "example3.php"; // set the DOAP Server URL
 
// this function will print our array(s) to the results div
function printarray( arr, depth )
{
	if (!depth) var depth=0; // depth is used to indicate sub-arrays (multi-dimensional)
	for (var i=0; i<arr.length; i++) // loop through the array
	{
		// if its a sub-array call this function again with an increased depth
		if (arr[i] instanceof Array) printarray(arr[i],depth+1); 
		else // otherwise output it
		{
			if (depth>0) // show -'s to indicate depth if above 0
			{
				for (var x=0; x<depth; x++)
				{
					document.getElementById('results').innerHTML += "-";
				}
				document.getElementById('results').innerHTML += " ";
			}
			// and display the content of the array element
			document.getElementById('results').innerHTML += (arr[i]+"<BR />");
		}
	}
}
 
 
function addition() // this will get parameters and call addition() on the server
{
	try
	{
		var params = Array(); // parameters are passed in an array
		params.push(document.forms.addform.one.value); // the first number
		params.push(document.forms.addform.two.value); // the second number
		var result = doap.call('addition',params); // call hello_world() on server passing params
	}
	catch(e) // catch any errors
	{
		// deal with any errors if it's a DOAP error or any other type of exception
		if(e instanceof DOAP_Error) alert("DOAP Error "+e.code+": "+e.desc);
		else alert("Unknown Error: "+e);
		return; // and exit the function if an error was encountered
	}
	// otherwise call printarray to put the results in the DIV element
	document.getElementById('results').innerHTML = "";
	printarray(result);
}
 
function multidim() // call multidim() on the server
{
	try
	{
		var result = doap.call('multidim');
	}
	catch(e) // catch any errors
	{
		// deal with any errors if it's a DOAP error or any other type of exception
		if(e instanceof DOAP_Error) alert("DOAP Error "+e.code+": "+e.desc);
		else alert("Unknown Error: "+e);
		return; // and exit the function if an error was encountered
	}
	// otherwise call printarray to put the results in the DIV element
	document.getElementById('results').innerHTML = "";
	printarray(result);
}
</SCRIPT>
</HEAD>
 
<BODY>
<DIV ID="results" CLASS="result_div">Results will appear here BY MAGIC!</DIV>
<BR /><BR />
 
<!-- This time we have a form to input two numbers which will be added by the server -->
<FORM ID="addform" onSubmit="return false;"> <!-- Start the form -->
 
<!-- The fields -->
<INPUT TYPE="TEXT" NAME="one" SIZE="3" VALUE="1"> + <INPUT TYPE="TEXT" NAME="two" SIZE="3" VALUE="2">
 
<!-- The Submit Button which in it's onClick calls the JavaScript addition() function -->
<INPUT TYPE="SUBMIT" VALUE="Calculate" onClick="addition();">
 
</FORM>
 
<BR /><BR />
 
<!-- The link to call the multidim to demonstrate multi-dimensional arrays -->
<A HREF="#" onClick="multidim();">Show a Multi-Dimensional Array</A>
<BR /><BR />
 
<!-- And a link to clean the results div back if needed -->
<A HREF="#" onClick="document.getElementById('results').innerHTML='<BR />';">Clear Results DIV</A>
 
</HTML>
 

The client defines controls to either call the addition() function with the set values or the multidim() function. Output is not just displayed but passed to the printarray() method which iterates through the array (and calls itself recursively on finding a sub-array) and displays the output.

<< ParametersContentsErrors >>