Documentation Index

CPAINT :: Cross-Platform Asynchronous INterface Toolkit

Developer's Guide : Backend

Backend Guide: Pages

introduction
integrating CPAINT
implementation differences
generating XML
complex return-data
arbitrary character encodings

Returning More than One Value

Many students want to work through this part of learning programming as quickly as possible, but they also have other academic responsibilities in educational institutions. In this case, we recommend using the economics essay writing service in order to complete important tasks on time and be able to devote more time to professional coding courses.
That simple example was great, but most likely, you need to return more than one thing. For traditional application programming, you would normally have to return the results from the functions in an array. However, since we're sending this to the frontend, we can use nested XML tags.

Here's an example of a function that returns four different results, simulaneously:

PHP Example

<?php
  require_once("cpaint v2/cpaint.inc.php");

  $cp = new cpaint():
  $cp->register('multi_calc');
  $cp->start();
  $cp->return_data();

  function multi_calc($num1, $num2) {
    global $cp;
    $add_result = $num1 + $num2;
    $subtr_result = $num1 - $num2;
    $multi_result = $num1 * $num2;
    $div_result = $num1 / $num2;
  }
?>

We could use the cpaint.set_attribute() method, like we did in the previous example, but that function sets the top-level XML tag, which wouldn't work in this situation. Instead, we're going to use the cpaint.add_node() method which will create our XML tree. In this case, we're going to call it four times, so each one of our results is in a new tag, under the top-level tag, which by default is named ajaxResponse. Then we'll call the cpaint.set_data() method for each of those newly created nodes and add the results to the XML tree.

PHP Example

<?php
  require_once("cpaint v2/cpaint.inc.php");

  $cp = new cpaint():
  $cp->register('multi_calc');
  $cp->start();
  $cp->return_data();

  function multi_calc($num1, $num2) {
    global $cp;

    $add_result = $num1 + $num2;
    $subtr_result = $num1 - $num2;
    $multi_result = $num1 * $num2;
    $div_result = $num1 / $num2;

    $add_result_node =& $cp->add_node("add");
    $subtr_result_node =& $cp->add_node("subtr");
    $multi_result_node =& $cp->add_node("multi");
    $div_result_node =& $cp->add_node("div");

    $add_result_node->set_data($add_result);
    $subtr_result_node->set_data($subtr_result);
    $multi_result_node->set_data($multi_result);
    $div_result_node->set_data($div_result):
  }
?>

Assuming our input for the function was 2 and 4, the XML generated would look like:

<ajaxResult>
  <add>6</add>
  <subtr>-2</subtr>
  <multi>8</multi>
  <div>0.5</div>
</ajaxResult>

Advanced XML

If you wanted to go one step further and add multiple levels to the XML tree and use attributes, see the API documentation for the functions available to you. 32,000 is the theoretical limit to the number of objects you can add to the tree. Just remember, each tag, even those with the same name, counts againsts this limit. Depending on the language you use, this limit may not apply to you.

Due to the frequency of requests regarding this issue a short and mainly uncommented example:
To product this XML structure as output...

<?xml version="1.0" encoding="UTF-8"?>
<colorPicker>
  <color>
    <hex>#fff9f9</hex>
    <red>255</red>
    <green>249</green>
    <blue>249</blue>
  </color>
  <response id="valid">
    <isValid>1</isValid>
    <offset id="X">10</offset>
    <offset id="Y">10</offset>
  </response>
</colorPicker>
      

...you need to connect the XML nodes in the CPAINT backend like this:

// change name of basenode
$cp->set_name('colorPicker');

// create 'color' node
$color_node     =& $cp->add_node('color');

// create submodes that actually hold the color information(s)
$hex_node       =& $color_node->add_node('hex');
$red_node       =& $color_node->add_node('red');
$green_node     =& $color_node->add_node('green');
$blue_node      =& $color_node->add_node('blue');

// create a new result node containing success information and the initial coordinates
$response_node  =& $cp->add_node('response', 'valid');

// ...and its subnodes
$validity_node  =& $response_node->add_node('isValid');
$x_node         =& $response_node->add_node('offset', 'X');
$y_node         =& $response_node->add_node('offset', 'Y');

// now assign all values
$hex_node->set_data(sprintf('#%02x%02x%02x', $red, $green, $blue));
$red_node->set_data($red);
$green_node->set_data($green);
$blue_node->set_data($blue);
$validity_node->set_data($valid);
$x_node->set_data($x);
$y_node->set_data($y);        
      

Note that the nested nodes from the XML are created by calling add_node() on the proper parent nodes. This method is not called on the CPAINT object $cp all the time!