Exact DOMAIN Issue

Somewhere along the line, Flash player seems to have changed its policy on accessing data from different domains.  I used to be able to run Falcon with either http://www.danzen.com or http://danzen.com but now, it matters.  So if you are using the URL with the www to get to the page then use www in the Falcon URL.  If you are not using www then do not use www in the Falcon URL.  Or you can use relative or virtual URLs.

Falcon Challenge! Is there a simpler way to get data from a database into a DataGrid in Flash?

CHALLENGE!

Here is the code required to get data from a MySQL database result in PHP into a DataGrid on stage in Flash. Below is script.php:

<?PHP 
   $db = mysql_connect("yourServer", "yourUserName", "yourPassword");
   mysql_select_db("yourDatabase",$db);
   
   include_once("falconize.php");  // in same directory
   echo falconize(mysql_query("SELECT * FROM yourTable",$db));   
?> 

In Flash with DataGrid on stage:

   // in package
   import com.danzen.utilities.FalconProvider;

   // in constructor or some other method
   var myFalcon:FalconProvider = new FalconProvider("script.php");
   myFalcon.addEventListener(Event.COMPLETE, function(e:Event) {       
      myGrid.dataProvider = e.target.dataProvider;
   });

We could be wrong, but is this the shortest number of lines around? Let us know if you find a challenger! Download the new falcon.zip for the added falconize.php file.

Dan Zen

-2010-

FalconProvider Correction

An update has been made to FalconProvider in the Falcon Zip. Thanks to Kasper Kamperman saving the lonely single-result return. Please update your FalconProvider files and recompile any swf’s that use it.

Falcon Flash Launches to Help Designers and Developers Pass Data Between Flash and Server Scripts

falconSample

Falcon Flash at https://falconflash.wordpress.com is the first of the Flash Feather series of advanced interface classes in Flash to launch.  With over one thousand downloads we are pleased that it has been helpful.  It really is a simple wrapper class for the five data classes in Flash.  It lets you easily read text and XML or send variables to and from server scripts like PHP.

var myFalcon:Falcon = new Falcon("yourfile.php or .xml or .txt");
myFalcon.addEventListener(Event.COMPLETE, getData);
function getData(e:Event) {
    trace (myFalcon.data.yourVariable);
    // or just myFalcon.data for XML or txt
}

Flash Feathers is located at http://flashfeathers.wordpress.com and through the links at the right.  Flash Feathers features the following open source solutions:

FLASH FEATHERS

  • ROBIN – multiuser chat and realtime game classes with Flash and PHP
  • FALCON – easy data transfer between Flash and server scripts
  • HUMMINGBIRD – parallax effect with mouse movement for 3D menus
  • GOOSE – multitouch emulator and processor with just Flash and Browsers
  • PENGUIN – tilt emulator and processor for tilt and translation apps
  • WOODPECKER – sound frequency and wave animation for MP3 and FLV
  • GOOSE – motion capture so a cursor follows your Web cam motion
  • DODO – blob detection to provide blobs anywhere there is Web cam motion

The Falcon Download ZIP file is at left.  There is a standard way to install all the Flash Feathers classes as detailed in the readme.txt file and the various class files and goes like follows:

INSTALLATION

  • create a classes/ folder somewhere generic on your harddrive
  • add the classes folder to your Flash Class Path – in the Flash Menu:
  • choose Edit > Preferences > ActionScript – ActionScript 3 Settings
  • use the + sign for the source path box to add the path to your classes/ folder
  • put the provided com/ folder in your classes/ folder

This way your classes from Flash Feathers will all work together along with other people’s distributed classes.  DO NOT add the com/ folder to your class path in the Flash preferences!  Read above if this is not clear.

Please have a look at the VIDEO page and the sample CODE page for more information about Falcon.  You can download the code through the link at the left on https://falconflash.wordpress.com.

FRIENDS AND DONATIONS
Please let us know if you use Falcon and we will add you to the Friends area at left.  If you use Falcon for a commercial venture and would care to donate that would be super!

karmatja

FEATHERS
Falcon is one of a selection of open source solutions called Flash Feathers available at http://flashfeathers.wordpress.com.

feathers

Dan Zen

-09-

Data Formats and Recommendations for Data Transfer Between Flash and PHP

 

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.

Combinations and Permutations of Hierarchy

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 fewest characters. 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 (https://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-

Falcon – Simple Flash AS3 Data Class – Keywords

Here is a list of keywords that have been used to find Falcon – Flash AS3 Data Class.

Click the Falcon press release link to find out more about how Falcon can help you get data in and out of Flash.  Here is a post that shows a simple example of sending variables back and forth between Flash and PHP.  Please see the examples link above for more examples.  Hope it helps!

 

flash cs3 xml 130
falcon flash 119
urlvariables 48
xml flash cs3 41
import xml into flash 39
load xml into flash 38
xml in flash cs3 38
as3 urlvariables 30
falcon as3 28
loading xml into flash 27
e.target.data 25
loading xml into as3 24
loading xml as3 24
flash as3 urlvariables 23
importing xml into flash cs3 22
load xml into flash cs3 20
xml and flash cs3 20
xml into flash cs3 19
as3 urlrequestmethod 19
urlrequest as3 18
flash urlloader 18
flash cs3 and xml 18
falcon icon 17
actionscript 3 load xml 17
flash falcon 16
flash cs3 load xml 16
urlvariables as3 16
as3 load xml text 15
load xml into flash as3 15
xml text in flash cs3 14
flash cs3 xml text 14
flash as3 load xml 13
as3 load xml 13
load xml in flash cs3 13
loading xml into flash as3 13
flash urlvariables 12
as3 urlloaderdataformat 12
loading xml into flash cs3 12
import xml flash cs3 12
flash as3 urlrequest 12
loading xml in flash 11
actionscript 3.0 load xml 11
xml text flash cs3 11
actionscript load xml 11
falconflash 11
load xml actionscript 3 11
urlvariables flash 11
as3 urlrequest 11
importing xml data into flash cs3 10
loading xml in as3

Passing Multiple Variables Between Flash and PHP

flashtophp1

Click here to download a simple example of passing multiple variables from Flash to PHP using the Falcon Class.  This has always been available with the Falcon class but the original variables example uses the FalconProvider class – and has the added complexities of a database (MySQL) and a DataGrid component in Flash.

This zip has the very basics of passing variables between Flash and the server.

This zip also removes all the examples provided in the main Falcon ZIP which may make it easier for some to organize themselves ;-)  If you want XML, Text, DataGrids or lists, etc. then get the original ZIP.

-09-

%d bloggers like this: