Building Data-Driven Flash Movies - Alien Invasion (
Page 5 of 8 )
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:
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?