Flash Text Database Tutorial: Get Total Records in Database
The database has been split into an Array in previous Flash ActionScript tutorial, it's time to see how to get the total number of records in the database.
Flash Tutorial Content:
In previous Flash ActionScript text database tutorial, we already learned how to split the database into an Array. Now we need to think how to get the records and then get the data from each record. The obvious solution is:
- loop through the records,
- split each record into an array.
Once the data are put into an array, they can be gotten easily. Therefore we need to know the total records in the database so that we can then loop through the records.
Length of Array:
myStringArray.length
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 myString:String;
var myStringArray:Array = new Array();
function loadComplete(evt:Event) {
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
output_txt.text = "There are total " + myStringArray.length + " records in the database";
}
//////////////////////////////
///// 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:
Remarks:
Now, we know how to get the total number of records in the database. In the next Flash ActionScript tutorial, we try to get the records of the database.