PDA

View Full Version : urgent! php with flash arrays



lira
December 30th, 2003, 08:32 AM
i'm not familiar with php at all and require help from you experts over here asap!

How can i use flash to read variables from a php page, and put them into an array? How must the variables be defined on the php page? Any example codes? Thanks.

λ
December 30th, 2003, 08:53 AM
use split

for example:



print "&var=hi;bye;cya";


flash:



//ought to work, though untested
var my_lv = new LoadVars();
my_lv.onLoad = function(){
_root.theArr = this.var.split(";");
}
my_lv.load("thePage");

then the variable theArr will have hi at position 0, bye at position 1 and cya at index 2 :)

lira
December 30th, 2003, 10:14 AM
Hi, I've done the following


playList_lv = new LoadVars();
playList_lv.load("playlist.txt");
playList_lv.onLoad = function(){
_root.playList1 = this.playlist.split(";");

}
trace(_root.playList1);


and for playlist.txt:
playlist=a;b;c

trace returns undefined.
but when i place trace inside the onload function,...

playList_lv = new LoadVars();
playList_lv.load("playlist.txt");
playList_lv.onLoad = function(){
_root.playList1 = this.playlist.split(";");
trace(_root.playList1);
}


trace gives me a,b,c. am i missing out something? i cant seem to access the array from outside onLoad() ...

hamza84
December 30th, 2003, 11:57 AM
Don't you have to declare an Array in the flash movie to do this?

comicGeek
December 31st, 2003, 01:27 PM
Take a look here:


playList_lv = new LoadVars();
playList_lv.load("playlist.txt");
playList_lv.onLoad = function(success){
if(success){
//to make things a little more
//understandable do it like this...
myPlaylist = this.playlist;
//instead of using a ";" use
//a "|" just to be safe...
_root.playList1 = myPlaylist.split("|");
//this will display the contents of the array...
trace(_root.playList1);
//this will display the contents of
// each element of the array...
trace("first element is: "+_root.playList1[0]);
trace("first element is: "+_root.playList1[1]);
trace("first element is: "+_root.playList1[2]);
}
}

Also your text file should be something similar to this:


&playlist=fisrtentry|secondentry|thirdentry;

The problem you encoutered with was due to the fact that you placed it outside the LoadVars object. Since you are tracing the _root.playList1, you have to place trace inside the LoadVars object. Flash reads all the code from top to bottom and when trace was executed the LoadVars object was not yet able to completely load the text file thus you get undefined.

eyezberg
January 1st, 2004, 08:37 PM
what that means, as for any load action in flash, is that it won't wait for the load to finish, but continue executing the following code after, so as correctly stated, your trace gets run before onLoad is true and the array defined...just use a check to see if the array exists, or a setInterval + clear to repeatedly check..