Home arrow Flash arrow Page 5 - Building Data-Driven Flash Movies

Alien Invasion - Flash

Contrary to what you might think, Flash isn't just all tweensand motion blurs. The application comes with powerful data-retrievalcapabilities, which allow you to hook your Flash movie clips up to adatabase (or any other external data source) and dynamically build movieclips that are easy to update and modify. Find out more, in this roughguide to the data-driven Flash universe.

  1. Building Data-Driven Flash Movies
  2. Message In A Bottle
  3. No Hands
  4. No News Is Good News
  5. Alien Invasion
  6. What's On The Menu?
  7. Splitting Up
  8. Linkzone
By: The Disenchanted Developer, (c) Melonfire
Rating: starstarstarstarstar / 71
July 23, 2002

print this article


Let's assume that the news headlines to be displayed are stored in a database table which looks something like this:

+---------------------+------------------------------------------------ | date | body +---------------------+------------------------------------------------| 2001-12-20 10:16:48 | An extra-terrestrial materialized outside the rabbit hole today. Calling itself ALICE, this ET claims to have come from a planet named ... | | 2001-12-20 10:20:58 | Executives at Robots'R'Us today announced the launch of their latest multi-function household assistant, IDA (I'll Do Anything), the most sophisticated ... | +---------------------+------------------------------------------------
The script "ticker.php" needs to connect to this database and extract the five most recent records from it. This data can then be converted into form-encoded data and picked up by the Flash movie.

Here's the code to accomplish this:

<? // ticker.php // open connection to database $connection = mysql_connect("localhost", "root", "secret") or die ("Unable to connect!"); mysql_select_db("news") or die ("Unable to select database!"); // formulate and execute query $query = "SELECT body FROM stories ORDER BY date DESC LIMIT 0,5"; $result = mysql_query($query) or die("Error in query: " . mysql_error()); if (mysql_num_rows($result) > 0) { // iterate through rows while($row = mysql_fetch_object($result)) { // build headline string $str .= $row->body . " * "; } // print output as form-encoded data echo "content=" . urlencode($str); } // close connection mysql_close($connection); ?>
Again, this is fairly simple. The script first connects to the database server, selects a database and executes an SQL query to select the five most recent stories from the system. A "while" loop is used to iterate over the returned resultset, with the content of each record appended to a single string, separated by an asterisk.

Once the entire string has been constructed, it is encoded via PHP's urlencode() function and sent to the standard output, where it can be picked up by the Flash movie.

If PHP isn't your cup of tea, you can also do this in Perl - here's the code you'll need:

#!/usr/bin/perl # load module use DBI(); # connect my $dbh = DBI->connect("DBI:mysql:database=news;host=localhost", "root", "secret", {'RaiseError' => 1}); # execute query my $sth = $dbh->prepare("SELECT body FROM stories ORDER BY date DESC LIMIT 0,5"); $sth->execute(); print "content="; # iterate through resultset while(my $ref = $sth->fetchrow_hashref()) { print "$ref->{'body'} * "; } # clean up $dbh->disconnect();
Here's what the output of the script might look like:

content=Executives+at+Robots%27R%27Us+today+announced+the+launch+of+thei r+la test+multi-function+household+assistant%2C+IDA+%28I%27ll+Do+Anything%29% test+multi-function+household+2C+t he+most+sophisticated+...+%2A+An+extra-terrestrial+materialized+outside+ he+most+sophisticated+the+ rabbit+hole+today.+Calling+itself+ALICE%2C+this+ET+claims+to+have+come+f rabbit+hole+rom+ a+planet+named+...+%2A+
With the server-side component in place, and the Flash movie all prepped and ready to go, all that's left is to test the movie again. This time, the text box should be dynamically populated with the results of the SQL queries in the script above, and should display this data as a scrolling news ticker along the bottom of the movie.

As the database is updated with new stories, the Flash file will automatically read and display them via the server-side script "ticker.php". Pretty cool, huh?

>>> More Flash Articles          >>> More By The Disenchanted Developer, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- An Overview of Flash and ActionScript
- Building Web Forms In Flash
- Building Data-Driven Flash Movies
- Flash 101 (part 6): The Final Countdown
- Flash 101 (part 5): Spiralling Out Of Control
- Flash 101 (Part 4): Don't Touch Me!
- Flash 101 (part 3): Bouncing Around

Developer Shed Affiliates


Dev Shed Tutorial Topics: