Logo         Community
The Company
Your Account
Contact Us
Building Data-Driven Flash Movies
Hook your Flash movie clips up to a database.

| Alien Invasion |

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

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
| 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

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:


# 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");

print "content=";

# iterate through resultset
while(my $ref = $sth->fetchrow_hashref())
        print "$ref->{'body'} * ";

# clean up

Here's what the output of the script might look like:

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '

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?

How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!

previous page more like this  print this article  next page
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
In cut!...
American Chai
The Core
Find out how you can use this article on your own Web site!

Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback