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: Loop Through Records in Database

Now we need to get the records of the database. An easy way is to loop thorugh all the records.

Please update flash player to view this Flash ActionScript tutorial!

Flash Tutorial Content:

In this Flash ActionScript text database tutorial, we try to loop through the records in the database. This allow us to get the records one by one. Let's see how to get the records.

Loop Through Records:
for (i=0; i<myStringArray.length; i++)

//The record in each row is: myStringArray[i]


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 myString:String;
var outputString:String;

function loadComplete(evt:Event) {

//Assign the loaded file into a string (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 = 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++)
outputString = outputString + "The " + (i+1) + " record is: " + myStringArray[i] + "<BR>";


//Display the string to Text Field
output_txt.htmlText = outputString;



///// 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-4.fla


Ah..... All records in the database can be obtained now with just a few line of Flash ActionScript codes. Now, we need to think how to get the data from the records. Please read on Flash ActionScript tutorials....