Home Services Industries Showcase Library People

 
 

 

4.1.8 "How Do You Get Data From Databases Into Flash?"

There are several ways to pull data into Flash.

 
Include the Data in the URL of the .swf

For example, let's say you have a movie called movie.swf. Where you would normally reference movie.swf in your HTML (there are 2 places for each .swf file), you could put:

     movie.swf?start=yesterday&end=tomorrow&user=johnny

If you've worked with HTML forms that use action="get", then you will be familiar with this syntax.

Doing this will create 3 variables on the main time line —"start," "end," and "user," with values "yesterday," "tomorrow," and "johnny," respectively.

In some cases, you can work with the variables being static on a page, but often you will want them to be dynamic. In each case you go into the code and add the variables you need to the code Flash/Dreamweaver has already created for you automatically.

We use the "static" approach for the nav bars on our pages. We pass variables that tell Flash which item should be highlighted and/or disabled. These are hard-coded on every page.

You can use a variety of server-side technologies to pass dynamic variables. Using Server-Side Include (SSI) and PHP you use an "echo" function to pass the value of a single variable. For example:

     movie.swf?start=<?php echo $start ?> (using PHP)

It's not quite so simple to get the value of a single variable using SSI. Now it's important that the value you pass be "URL-encoded." That means there can't be any characters that would not otherwise be allowed in a URL. PHP makes this simple...

     movie.swf?start=<?php echo urlencode($start) ?>

Flash will automatically decode the values — so it's only something you have to worry about when you're working in the HTML. You can also pass all the variables which the page received.

     movie.swf?<!--#echo var="QUERY_STRING"--> (using SSI)
     movie.swf?<?php echo $QUERY_STRING ?> (using PHP)

Here you would not want to use urlencode() because $QUERY_STRING (an environment variable) is already URL-encoded.

By using SSI and PHP as examples, we're not implying that they are the only technologies you can use. Most server-side web technologies can do similar things. The objective is achieved by modifying the HTML that the user receives.


Retrieving URL-encoded and XML-formatted Documents

The other ways of getting data into Flash involve the Flash movie requesting the data from a middleware application.

Flash can handle 2 types of data files — URL-Encoded text, and XML. Flash simply does an HTTP request to get the data. For URL-encoded text data are requested using the loadVariables function. For XML, this is accomplished using an XML object and XML.load (plus you have to parse the XML object).

The contents of a URL-encoded document would look something like:

     start=yesterday&end=tomorrow&user=johnny

This should look familiar. Flash is fairly lenient about the formatting of the document — Flash will process many "invalid" XML documents.

The contents of an equivalent XML document would look something like:

     <?xml version="1.0" encoding="UTF-8"?>
     <doc>
          <start>yesterday</start>
          <end>tomorrow</end>
          <user>johnny</user>
     </doc>

The above example shows one way the XML document could be formatted, there are other possibilites. Just as Flash is lenient about the formatting of the URL-encoded documents, it's also lenient about the formatting of XML documents. Because the strength of XML is that it can be used by a variety of applications, we recommend that XML documents be formatted properly.

So where do URL-encoded and XML documents come from? They can be static files on disk or they can be created by any server-side technology that can connect to your database. This can be PHP, ASP, JSP, Java Servlets, a CGI script, or any number of other technologies. Applications that perform this function are called "middleware" applications.

It's important to note that it's best if you can pass the data by embedding it in the HTML, because you thereby reduce the number of hits on your server.

There are times when embedding the data in the HTML page is not a good idea, for example: 1) when the data are sensitive and you don't want them shown if someone does a "View Source"; and 2) if your Flash application needs to update the data after the movie is loaded.


Establish an Open Socket With the Server

The last way that Flash can get data is using "XML sockets."

Imagine there is an important event that happens once every day, and you need to know about it as soon as it happens. You could call a contact person every 5 minutes to see if it has occurred, but then your competitor might have a 4.5 min lead on you, and you'd annoy the person you were calling. Or you could give the appropriate contact person your pager number and know instantly. Getting a URL-encoded or XML-formatted document is like continually calling the contact person; XML sockets are analogous to leaving your pager number.

Why it's called an XML socket is anyone's guess; it's just a simple HTTP socket. A socket is simply an open connection ("conversation") between 2 applications, in this case your Flash application and a middleware application. You can actually use any protocol and formatting you like with an XMLSocket; it is not limited to XML.

Not every middleware application can support sockets. In fact, most probably don't. Unless you're working with time-sensitive data (like stock tickers), you'll probably never need to deal with a socket.