Flash AS3 Tutorials Center | Interesting Flash Animation Tutorials | Valuing Flash Animation Tutorials | Flash ActionScript and Animation Home

AS3 Beginner Tutorials | AS3 Basic Lessons | AS3 Valuing Courses | AS3 Components Tutorials | AS3 and PHP Interaction Tutorials
AS3 Practical Tutorials | AS3 Animation Techniques | AS3 Transition Effects Tutorials | AS3 Download Upload Files | AS3 Particle Systems
Communication Between Flash Movies with AS3 | AS3 and JavaScript interaction | AS3 Matrix Transformation | AS3 Physics Simulation Tutorials

Flash Text Database Tutorial: Get Data from Records

The records of the database were split into an Array in previous Flash ActionScript tutorial. In order to retrieve the data, the record need to split into another Array. Let's see how to do it.

Please update flash player to view this Flash ActionScript tutorial!

Flash Tutorial Content:

Now, everything is ready:

- database loaded to Flash Movie and was split into an array,

- the records can be obtained by looping through the databse.

It's time to think how to get the data from the record. This is obvious that we can also split the records to an Array. Once the data is in an Array, they can be played around easily.

Split Records into Array:
recordArray = myStringArray[i].split("|");

The complete Flash Movie is shown as above, you may try how it works before you start this tutorial.

Flash ActionScript Codes:

//You can declare the url of the text file as string
//so that you can use it later
var url:String = "staff.txt";

//Create a URLLOader object with the name myLoader
var myLoader:URLLoader = new URLLoader();

//Load the text file by using URLRequest
//Can be local file or URL of webpage
myLoader.load(new URLRequest(url));

//Listen when the loading of text file COMPLETE
//Call the loadComplete function when the loading COMPLETE
myLoader.addEventListener(Event.COMPLETE, loadComplete);


//Declare some variables to use later
var myStringArray:Array = new Array();
var recordArray:Array = new Array();
var myString:String;
var outputString:String;

function loadComplete(evt:Event) {

//Assign the loaded file into a string (myString)
//Let's explain it more details
//myString now is like:
staff name 1|Title 1|Department 1

staff name 2|Title 2|Department 2

staff name 3|Title 3|Department 3

staff name 4|Title 4|Department 4
//There is a line carriage return and a line feed between the
//records in myString
myString = evt.target.data;

//Remove Listener that no longer in use
myLoader.removeEventListener(Event.COMPLETE, loadComplete);

//Split the myString (text database) into an Array
//We learned from previous tutorial, there are line carriage return and
//line feed between each record. Therefore we can use the line carriage
//return and line feed (i.e. \r\n) as the Item delimiter and split the
//text database into an Array.
// \r is the line carriage return
// \n is the line feed
//myStringArray now is like:
[staff name 1|Title 1|Department 1,
staff name 2|Title 2|Department 2,
staff name 3|Title 3|Department 3,
staff name 4|title 4|Department 4]
myStringArray = myString.split("\r\n");

//myStringArray is now an Array
//You can now play around whatever you like.
//Of course you need to know the total records in the database,
//so that you can loop through each record. We will show how to
//loop through each record in coming tutorial.
//At this moment, let's see how to get the total records:
//myStringArray.length is the total records in the database
//We don't need to print out the result in this tutorial
//output_txt.text = "There are total " + myStringArray.length + " records in the database";

//Set the multiline parameter of the Text Field to true
//The <br> tag creates a line break in the Text Field.
//Must set the text field to be a multiline Text Field to use this tag.
output_txt.multiline = true;

///// Loop through the records /////

//Assign an empty value to this string (outputString)
//This string is used to display the result
outputString = "";

//Declare i as an integer
var i:int;

//Execute the For Loop Statement
//A For Loop requires three expressions:
//a variable i is set to an initial value - zero
//a conditional determines when the looping ends -
//when i < total number of records (i.e. length of myStringAtrray)
//an expression changes the value of the variable with each loop -
//i++ (increase by one)
for (i=0; i<myStringArray.length; i++)
//This output is used in previous tutorial
//outputString = outputString + "The " + (i+1) + " data set is: " + myStringArray[i] + "<BR>";

//Split each record into an Array
//Each record is in the format name|title|department
//The item Delimiter is "|" in this example record
//recordArray[0] is the name
//recordArray[1] is the title
//recordArray[2] is the department
recordArray = myStringArray[i].split("|");

//Let's link up the record and display in the message box
outputString = outputString + "Staff is " + recordArray[0] + " , with title of " + recordArray[1] + " , working in " + recordArray[2] + " Department" + "<BR>";



///// ERROR HANDLING /////


// Listen error events for the loading process
// Call loadError function in case an error appear
myLoader.addEventListener(IOErrorEvent.IO_ERROR, loadError1)
myLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, loadError1)
myLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, loadError2)


function loadError1(event:ErrorEvent):void {
// Display error message to user in case of loading error.
output_txt.text = "Sorry that there is an error during the loading of an external text file. The error is:" + "\n" + event.text;


function loadError2(event:HTTPStatusEvent):void {
// Display error message to user in case of loading error.
output_txt.text = "Sorry that there may be an error during the loading of an external text file. The error is:" + "\n" + event.status;

Download Flash Source File:

Flash Source File text-database-5.fla


Now, all data in the database can be extracted and play around. It is only up to you how to use the data for your Flash program. In the last Flash ActionScript tutorial of this text database series, we will design a very simple program to show how to use the data.