Official website for Web Designer - defining the internet through beautiful design
FOLLOW US ON:
Subs House Ad
Mar
26

Create a poll and chart component in Flash CS3

by Dave Harfield

12 Get the data

Using the URLLoader class, create a new loader instance and assign a constant to the dataFormat property to tell it variables will be returned. Then add a Event Listener to the loaderComplete event and call the file to load using the load method. Next, create a loaderCompleteHandler function, assign the input event.target.data and variable name to the colour totals, and finally update the chart with the new totals.

get data from database
*/
var loader:URLLoader;
loader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.addEventListener(Event.COMPLETE,
loaderCompleteHandler);
loader.load(new URLRequest(“http://www.playfool.
com/magfiles/wd150/getPoll.php”));
// complete handler
function loaderCompleteHandler(event:Event):void {
yellowtotal = event.target.data.yellow;
bluetotal = event.target.data.blue;
orangetotal = event.target.data.orange;
redtotal = event.target.data.red;
// set chart at the start
updateChart();
}

13 Setting the totals

You have managed to retrieve the bar totals but you still need to set the new totals after a user has made a selection. Similar to the previous step, create a new URLLoader and an Event Listener. This time, create a new function that calls the PHP file setPoll.php, but also send variables with it using the URLVariables class. Finally, call the function buttons(), parsing in the value ‘false’.

var loaderData:URLLoader= new URLLoader();
loaderData.dataFormat = URLLoaderDataFormat.TEXT;
loaderData.addEventListener(Event.COMPLETE, d
ataLoaderComplete);
function loadData():void {
var request:URLRequest = new
URLRequest(“http://www.playfool.com/magfiles/
wd150/setPoll.php”);
var variables:URLVariables = new
URLVariables();
variables.red = redtotal;
variables.yellow = yellowtotal;
variables.orange = orangetotal;
variables.blue = bluetotal;
request.data = variables;
loaderData.load(request);
// enable all buttons
buttons(false);
}

14 More buttons

Finally for the Flash part, create the function that’s called when the setPoll.php triggers that it has completed, which will call another function called buttons(), and parse the value ‘true’. Create the function buttons and set all the buttons’ enabled property to the parsed-in parameter. This will ensure you are not clicking the buttons until the database has been updated. You’re not finished yet, though, head over to the boxout to create the PHP files.

function dataLoaderComplete(event:Event):void {
// enable all buttons
buttons(true);
}
function buttons(b:Boolean):void {
red_btn.enabled = b;
yellow_btn.enabled = b;
orange_btn.enabled = b;
blue_btn.enabled = b;
}

Update your MySQL database
Complete this tutorial by creating the PHP files to read and update the database

First of all, create a new MySQL database via your hosting control panel. This will give you the host name for the variable $Host. You can also get the other details you need from here. Create a table called ‘poll’ with four rows (red, yellow, orange and blue), all of type int.
Open up a script editor and create a new PHP file called ‘getPoll.php’. Add the script below, replacing the variables $Host, $User, $Password and $DBName with your details.

<?
/*
Produced by Darren Richardson
for Web Designer Article
play@playfool.com
file: getPoll.php
*/
// Set up variables
$Host =”host name”;
$User = “username”;
$Password = “password”;
$DBName = “database name”;
// Set up connection string
$link = mysql_connect ($Host, $User,
$Password);
// Set the database name
mysql_select_db($DBName);
// Sql to retrieve the record if it exists ;
$sql = ‘SELECT * FROM poll’;
// get the resultset
$Result = mysql_query($sql);
// assign the variable num_rows to
the total amount
of records returned.
$num_rows = mysql_num_
rows($Result);
while ($row = mysql_fetch_
assoc($Result)) {
print ‘amt=4&’;
print ‘red=’ .$row[‘red’].’&’;
print ‘yellow=’ .$row[‘yellow’].’&’;
print ‘orange=’ .$row[‘orange’].’&’;
print ‘blue=’ .$row[‘blue’];
}
// close the database connection
mysql_close($link);
?>

The next file is setPoll.php, which takes in the four parameters parsed from Flash and updates the database with those amounts.

<?
/*
Produced by Darren Richardson
for Web Designer Article
play@playfool.com
file: setPoll.php
*/
// Set up variables
$Host =”database host”;
$User = “username”;
$Password = “password”;
$DBName = “database name”;
// Set up connection string
$link = mysql_connect ($Host, $User,
$Password);
// Set the database name
mysql_select_db($DBName);
// create the SQL
$sql = ‘UPDATE `poll` SET red=’ .
$_REQUEST[‘red’] .
‘, yellow=’ .$_REQUEST[‘yellow’] . ‘,
orange=’
.$_REQUEST[‘orange’]. ‘, blue=’ .
$_REQUEST[‘blue’];
// set the resultset
mysql_query($sql);
// close the database connection
mysql_close($link);
?>

Upload the PHP files to the same directory location as the Flash movie on your server and you’re then ready to test your Flash poll.

Pages: 1 2 3

Bookmark and Share

11 Comments »

What's your opinion?

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.