all groups > flash data integration > july 2005 >
You're in the

flash data integration

group:

Writing an AS Loop to read database records


Writing an AS Loop to read database records wavereflections
7/18/2005 9:57:19 PM
flash data integration: Hi,

How could I write a loop to simplify the code below which is reading records
from a MS Access database (using ASP)? I'm showing only 3 iterations but I'd
like to read the entire database. I can read a variable from the database
named 'TotalRecords' so I'd like to know how that gets incorporated.

Thanks!

CurrentRecord=0
var jpg:String = ".jpg"

myData = new LoadVars()
myData.load("GetData.asp?Record=0",this)
myData.onLoad = function(succes){
if(succes){
holder_mc0.loadMovie(this.Availability + jpg)
} else trace("Error loading data")
}

myData = new LoadVars()
myData.load("GetData.asp?Record=1",this)
myData.onLoad = function(succes){
if(succes){
holder_mc1.loadMovie(this.Availability + jpg)
} else trace("Error loading data")
}

myData = new LoadVars()
myData.load("GetData.asp?Record=2",this)
myData.onLoad = function(succes){
if(succes){
holder_mc2.loadMovie(this.Availability + jpg)
} else trace("Error loading data")
}
Re: Writing an AS Loop to read database records the dude881
7/19/2005 12:00:00 AM
Another way that you can simplify database is make your server page output the
records as XML since flash can easily consume xml packets. Also here is a great
tutorial for reading a database with ASP/Flash (MSACCESS):
http://www.macromedia.com/devnet/mx/flash/articles/flashpro_asp.html. Hope this
provides help .
Re: Writing an AS Loop to read database records LuigiL
7/19/2005 12:00:00 AM
hmm, noticed a typo.
Re: Writing an AS Loop to read database records LuigiL
7/19/2005 8:13:43 AM
As you know LoadVars receives a string in the form
id0=0&name0=joe&age0=16&id1=1&name1=patrick&age1=18&
You cannot loop over these 'records' because they are not 'identified' as such
by the LoadVars class. They are just a bunch of variables. So, you have to
build an array in Flash of the incoming data and then you can loop over the
elements in the array. Either you split the results in elements or you loop
over all the vars in the returned string to build the array.

1. Split the results
The string you return to Flash could be like this:
&results=0,joe,16,1,patrick,18
In Flash you build the array:

// array to hold the content
var content_array:Array=new Array();

// new LoadVars object to receive data
dataReceiver = new LoadVars();
dataReceiver.onLoad = function() {
content_array = this.content.split(","); // build an array of the
results using , as a seperator
for(i=0;i<content_array.length;i++){ // loop over the elements in the
array
myText_txt.text+=content_array+newline;
}
};
// Load the data
dataReceiver.load("http://www.yourdomain.com/yourfile.asp");

2. Loop over the vars in the returned string:
You're returned string could look like this:
id0=0&name0=joe&age0=16&id1=1&name1=patrick&age1=18&

Loop over the vars and build the array:
// array to hold the content
var content_array:Array=new Array();

// new LoadVars object to receive data
dataReceiver = new LoadVars();
dataReceiver.onLoad = function(){
// clean all previous text
myText_txt.text="";
for(var prop in this){
if(prop != "onLoad"){
content_array.unshift(this[prop]);
//trace(content_array);
}
}
trace(content_array.length);
for(var i=0;i<content_array.length;i++){
myText_txt.text+=content_array+newline;
}
};
// Load the data
dataReceiver.load("http://www.yourdomain.com/yourfile.asp");

To get all the results from the database you use
myData.load("GetData.asp",this) and have your ASP-file return a string with all
the results from the database.

Re: Writing an AS Loop to read database records mxc
7/19/2005 9:50:55 PM
Why not just get the asp file to send the total number of records as a varible
for example "n" and then extract the records with a loop inside the loadVars
object
eg.

var getNames:LoadVars = new LoadVars();
getNames.onLoad = function () {
for(var i:Number = 1; i < this.n; i++) {
name_holder.attachMovie("name_mc", "name_mc" + i,
name_holder.getNextHighestDepth(), initNames);
name_holder["name_mc" +i].case_name.text = this["case_text"+i];

}
}

Here I am taking a movie from the library containg a dynamic txt field and
placing it on the stage so I can create buttons which links to the full record-

Hope it can be adapted to your needs ;)
Re: Writing an AS Loop to read database records wavereflections
7/20/2005 12:00:00 AM
:) Thanks for all the examples! They have been most helpful.
Re: Writing an AS Loop to read database records LuigiL
7/20/2005 12:00:00 AM
Re: Writing an AS Loop to read database records LuigiL
7/20/2005 10:25:14 AM
That is a solution too. Then the code in the example would be like attached.
Differences: with this last solution you have to hard code the names of the
variables. When looping over the vars in the LoadVars object in the second code
example you can change the structure of the database without any problems in
the front-end.



// returned vars
n=2&id0=0&name0=joe&age0=16&id1=1&name1=patrick&age1=18&

var dataReceiver:LoadVars = new LoadVars();
dataReceiver.onLoad = function(){
// clean all previous text
myText_txt.text="";
for(var i=0; i<this.n; i++) {

myText_txt.text+=this["id"+i]+newline+this["name"+i]+newline+this["age"+i]+new
line;
}
}

// Load the data
dataReceiver.load("http://www.yourdomain.com/yourfile.asp");
AddThis Social Bookmark Button