INTRODUCTION
This post describes various formats of data and their similarities across different systems. It is recommended reading for beginners and long term coders. It will introduce philosophical principles and use specific examples of data transfer between Flash and PHP.
There are recommendations on how to transfer data later on in the post but before the recommendations will make sense, we need to explore data formats.
DATA
For data to be useful we usually provide a label for the data. This is called meta data or data about the data! Below, name is the meta data and Dan is the data.
name=Dan
This is how variables work in coding – you have a variable name and a variable value. If you have more than one variable you can string them together with a given character – called a delimiter. Below is called CGI format and we see it on the end of URLs:
name=Dan & gender=male & occupation=inventor
This data holds information about a person and lists various properties of that person. You can do this for any physical or virtual object and hence, Object Oriented Programming has objects with properties:
{name:"Dan", gender:"male", occupation:"inventor"};
Above is an object literal in Flash. In PHP associative arrays (Hashes) are used:
array(name=>"Dan", gender=>"male", occupation=>"inventor");
Note: that all these hold the same data and meta data – they just have different syntax.
MULTIPLE OBJECTS
Often we want to keep data on multiple objects:
name=Dan & gender=male & occupation=inventor &
name=Jen & gender=female & occupation=inventor
With variables, the name of the variable needs to be unique. So we put an index number on the variable names like so (start at 0 or start at 1):
name0=Dan & gender0=male & occupation0=inventor &
name1=Jen & gender1=female & occupation1=inventor
In Flash, we hold multiple objects in an array. An array is a list of elements. We can access the elements by their index number. This index number starts at 0 for almost all programming languages. Below is an array literal of two objects, me and you.
people = [me, you];
trace (people[0]); // would trace me
trace (people[1]); // would trace you
Now, remember the objects had name, gender and occupation properties:
people = [
{name:"Dan", gender:"male", occupation:"inventor"},
{name:"Jen", gender:"female", occupation:"inventor"}
]
In PHP this would be a multidimensional associative array (an array of arrays):
people = array(
array(name=>"Dan", gender=>"male", occupation=>"inventor"),
array(name=>"Jen", gender=>"female", occupation=>"inventor")
);
DATABASES
To store multiple objects we typically use a database which is just a table with rows (records) and columns (fields). Below we have two rows and three columns:
Name Gender Occupation
---- ------ ----------
Dan male inventor
Jen female inventor
To get the data out of the database table we loop through the rows and put the data into variables (CGI format) or into a PHP array or into XML as will later be described.
HIERARCHIES
This is one of the most important aspects of data and you will live in delusion if you do not fully understand. All data can be represented by a hierarchy. Hierarchies are often thought of as rigid but they are not because you are free to adjust the context.
In a hierarchy, an object is called a node – think of a node as a holder. Node 0 is at the top – it holds all (http://nodism.org). You can organize / categorize / group the object by any of its properties – so you have the various permutations (ors) and combinations (ands). Ors go across and ands go down. Below is a hierarchy diagram showing the location of the object me based on some of my properties.

Relative to any node, you have its context – which is above the node and its content which is below the node. A node also has a parent and can have children and siblings.
Note that the object we are interested in (me) appears in any of the nodes. It is nice that I am directly related to Node 0 (ring a bell?)
I am categorized under dans. I am categorized under inventors and inventors called dan. I am categorized under males and male inventors called dan, etc.
Imagine that we had 100 inventors and 100 gardeners. In the CGI format, we would have to repeat this information 100 times for each.
name=Dan & gender=male & occupation=inventor &
name=Jen & gender=female & occupation=inventor &
etc.
It would take up less data if would could say, here are all the inventors and here are all the gardeners.
inventors:
name=Dan & gender=male
name=Jen & gender=female
etc.
gardeners:
name=Jim & gender=male
etc.
Note the tabbing or nesting of the data. This is an indication that a hierarchy is present. It shows up in table of contents, in directory structures, etc. All these are just another view of a hierarchy. Relational databases try to cut down on this repetition and handle hierarchy but it is not always practical.
Short of applying prefixes to variables, there is no native way to create a hierarchy with CGI format. We call this flat data. But we can nest with multidimensional arrays in Flash and in PHP – Here is Flash:
people = [
{occupation:"inventor",
people:[
{name:"Dan", gender:"male"},
{name:"Jen", gender:"female"}
]
},
{occupation:"gardener",
people:[
{name:"Jim", gender:"male"}
]
}];
here is PHP:
people = array(
array(occupation=>"inventor",
people=>array(
array(name=>"Dan", gender=>"male"),
array(name=>"Jen", gender=>"female")
)
),
array(occupation=>"gardener",
people=>array(
array(name=>"Jim", gender=>"male")
)
));
Obviously, going from the simple tabbed list to arrays adds some complexity in syntax but the hierarchical structure is maintained. To simplify and standardize the representation of data in a hierarchy XML was created.
XML IS ANOTHER WAY TO STORE A HIERARCHY
The XML below has the same data and hierarchy as the arrays and objects above.
<people>
<occupation type="inventor">
<person name="Dan" gender="male" />
<person name="Jen" gender="female" />
</occupation>
<occupation type="gardener">
<person name="Jim" gender="male" />
</occupation>
</people>
A comparison on the structural characters used in the systems is below:
XML uses: <>/="
PHP uses: array()=>",
Flash uses: []{}",:=
XML is simpler. It is a little longer because you have to end your tags with tag names but that sometimes helps clarify what level you are on in your hierarchy.
SORTING
Before creating XML or multidimensional associative arrays you will want to decide how to categorize your data. This is just choosing your branch in the single hierarchy. Do you want to sort by occupation or by gender or by occupation and then gender:
<people>
<occupation type="inventor">
<gender type="male">
<person name="Dan" />
</gender>
<gender type="female">
<person name="Jen" />
</gender>
</occupation>
<occupation type="gardener">
<gender type="male">
<person name="Jim" />
</gender>
</occupation>
</people>
Sorting is usually determined by how you want to use or display your data. These days, with E4X XML parsing in Flash, it is pretty easy to recreate any branch of data so ultimately, the best way to sort your XML is so that it contains the least data. The XML above has 277 characters where the previous version was 217. So choose the previous. A flat version would look like below with 194 characters. So for three records it is not worth nesting. But you start saving at five records.
<people>
<person name="Dan" gender="male" occupation="inventor" />
<person name="Jen" gender="female" occupation="inventor" />
<person name="Jim" gender="male" occupation="gardener" />
</people>
CONSIDERATIONS
In general, what we are aiming for is the smallest amount of data being passed. There are trade-offs primarily in the usage of meta data. Do not send meta data if you want the smallest amount of data. Arrays can hold data in a hierarchy with or without meta data. But without meta data it can get confusing because you have to know where things are positioned. In essence, the index numbers become your meta data.
Textual data is small compared to image data like for a picture. So we usually do not mind sending meta data. Re-evaluate this for thousands of records for instance. Or at least keep your meta data (and data) small: n1=dan&g1=m&o1=inventor&n2=Jen etc.
A second consideration is ease of application which is sometimes related to lines of code required to do data transfer. Below are the three popular data formats as described above:
1. Variables
2. Arrays / Objects
3. XML
These are all relatively easy if the data is small and unrelated:
// variables
backgroundColor=#AAAAAA&width=500
// associative array
$config = array(backgroundColor=>"#AAAAAA", width=>"500");
// object
config = {backgroundColor:"#AAAAAA", width:"500"};
// XML
<config>
<background color="#AAAAAA" />
<width value="500" />
</config>
For multiple data you loop through data. Here is a sample of looping through a MySQL data result for each of the three types:
// variables
$i = 0;
while ($myrow = mysql_fetch_array($result)) {
echo "name".$i."=".urlencode($myrow['name']).
"&gender".$i."=".urlencode($myrow['gender']).
"&occupation".$i."=".urlencode($myrow['occupation'])."&";
$i++;
}
// multidimensional associative arrays
$config = array();
while ($myrow = mysql_fetch_array($result)) {
array_push($myrow, $config);
}
// XML
$config = "<config>";
while ($myrow = mysql_fetch_array($result)) {
$config .= '<person name="'.urlencode($myrow['name']).'
" gender="'.urlencode($myrow['gender']).'
" occupation="'.urlencode($myrow['occupation']).' />';
}
$config .= "</config>";
To create a hierarchy as you loop is more tricky. You do not do this with variables. XML for instance it looks like below – multidimensional associative arrays would be similar:
$query = "SELECT * FROM people ORDER BY occupation, name";
$result = mysql_query($query,$db);
$lastoccupation = "";
$output = '<people>';
if ($result) {
while ($myrow = mysql_fetch_row($result)) {
$name = $myrow["name"];
$gender = $myrow["gender"];
$occupation = $myrow["occupation"];
if ($occupation != $lastoccupation) {
if ($lastoccupation != "") {
$output .= '</occupation>\n';
}
$output .= '<occupation type="$occupation">\n';
$lastoccupation = $occupation;
}
$output .= '<person name="$name" gender="$gender" />\n';
}
$output .= '</occupation>';
}
$output .= '</people>';
So, not a lot of fun to form hierarchical data. You may be able to find custom PHP classes that will help you form hierarchical data but then there is the step of preparing the classes and calling them. In the end, the flat data will probably do for most cases unless you have hundreds of records. We have a surprise for you to make flat data easy in PHP.
One more consideration is JSON – JavaScript Object Notation. JSON is an alternative to XML as a way to pass a hierarchy. It does this by turning multidimensional associative arrays (arrays and objects) into a delimited string in one system (like PHP or Flash) so you can pass the data to a receiving system. In the receiving system, JSON parses (reads) the string and turns it back into multidimensional associative arrays (arrays and objects).
You send the JSON string as a variable value.
OPTIONS
Now that we have explored data, here are some options to transfer data.
1. Native to Flash, is the URLLoader and associated classes such as URLRequest, etc. This can get as complicated as 5 classes but can also be done in the two classes above taking various defaults (you’ll need an Event class too). URLLoader can pass variables, XML or JSON.
2. Falcon (http://falconflash.wordpress.com) is a way of transferring data such as variables, XML and JSON. It does so in one class and is a wrapper class for the native Flash data classes. Falcon also has a PHP function called falconize which in one line, turns a data result into numbered variables that FalconProvider automatically converts to a Flash DataProvider for populating List, ComboBox and DataGrid objects.
3. Flash Remoting is a way to call functions between systems. You can pass parameters to the functions and return results. The parameters and results can be variables, XML, and multidimensional associative arrays (arrays and objects). See AMFPHP. Remoting is a binary protocol so uses less data than string based operations. You probably will not notice until you are in hundreds if not thousands of records. Remoting requires classes on the Flash and PHP side and directory systems to be put in place. So it can be initially more complex but those that use it and get used to it like it.
RECOMMENDATIONS
Assuming you have users and a publisher (author or client, etc.)
1. Store publisher-updated variables in XML and collect with Falcon
2. Store user-updated data in a database and send variables with Falcon
3. Use FalconProvider and falconize for related, tabular data
4. For large numbers (over 100) of nested data use JSON and Falcon
5. For really large numbers (over 1000) of nested data send arrays with remoting
More on recommendation 3:
In PHP, the falconize function automatically numbers your database query results and echoes them to Flash in the CGI Format. It does this in one line as shown below. $db is your database connection, $error is any errors you may have recorded inputting data before this call.
echo falconize(mysql_query("SELECT * FROM table",$db), $error);
Flash List, ComboBox and DataGrid objects use a DataProvider object to populate their displays. A DataProvider is a linear array of objects – one object per row. Each object has properties to hold the values for each column in the row. We have seen this before – the linear array of objects:
myDataProvider = [
{name:"Dan", gender:"male", occupation:"inventor"},
{name:"Jen", gender:"female", occupation:"inventor"}
];
FalconProvider automatically makes a DataProvider for you so in your Event.COMPLETE method you can assign this ready-made DataProvider to your DataGrid for example as follows:
myDataGrid.dataProvider = myFalconProvider.dataProvider;
This means, with FalconProvider, getting data from a database result to a DataGrid is as easy as getting a single variable – and that is pretty easy!
1. Import your class
2. Call your server (PHP)
3. Set Event Handler for data coming back
4. Access the data coming back
SUMMARY
In this post:
- an explanation of the differences between flat (variables) and hierarchical data (multidimensional arrays and XML).
- the isomorphism of multidimensional associative arrays and XML (they are the same).
- arrays and objects can be passed by converting to JSON or using remoting.
- an alternative is XML.
- the native Flash URLLoader class and associated classes can send and receive variables, XML or JSON as can Falcon which uses one class.
- falconize lets you echo numbered variables matching your database results in one line of code in PHP.
- FalconProvider has added functionality of automatically making a DataProvider for List, ComboBox and DataGrid objects.
- recommendations on what system to use resulted in choosing Falcon except for very large data sets when you might consider remoting for speed.
Inventor, Dan Zen
-2010-
Filed under: Analysis Tagged: | adobe, amfphp, combobox, cs5, data, data formats, datagrid, dataprovider, detailed, explanation, Falcon, flash, hierarchy, json, list, nodism, philosophy, php, recommendation, remoting, simple, simple explanation, transfer, URLLoader, URLRequest, variables, XML









