brndn
June 9, 2008, 12:56pm
1
Hi all
I am have found a few examples on the net of how to load an array from a php generated script and then turn it into a flash array.
using this method
var thearray=new Array();
var recvars=new LoadVars();
recvars.onLoad=function(success){
if(success){
thearray=this.thestring.split("|");
// thearray.push(recvars);
trace(thearray);
LOADTEXT.text = thearray;
trace(thearray);
}
};
recvars.load("http://mysite/returnarray.php");
to access this php file
<?php
$thearray=array('item0','item1','item2','item3');
$thestring=implode($thearray,','); //makes: item0|item1|item2|item3
echo "&thestring=$thestring";
?>
it seems to work ok except the fact that I can not call individual elements of the array??
like this
trace(thearray[1]);
so is it creating a well formed array???
it spit out undefined.
brndn
June 9, 2008, 12:58pm
2
ok so trace(thearray[0]); spits out the entire elements.
so I am presuming I will have to push each item into the array.
but how do I loop through the total array echoed from php??
Your implode statement is wrong. The the array should be the second parameter. Also, you have set the “glue” to be a comma, not a pipe.
In the php section, I would send flash the number of elements that will be coming over like so:
<?php
$thearray=array('item0','item1','item2','item3');
//
$numitems = count($thearray);
//
$thestring=implode('|',$thearray); //makes: item0|item1|item2|item3
echo "&numitems=$numitems&thestring=$thestring";
?>
Then, you could set up a for loop to add the elements to the AS array
brndn
June 9, 2008, 1:19pm
4
hey thanks for that.
so my as loop would be like this??
brndn
June 9, 2008, 1:21pm
5
recvars.onLoad=function(success){
if(success){
thearray=this.thestring.split("|");
for(i=0; i<thearray.length; i++){
theNEWarray.push(thearray);
}
LOADTEXT.text = thearray;
trace(thearray[1]);
}
};
Sorry, can’t help with that part. My AS skill are akin to a mentally impaired lemur.
brndn
June 9, 2008, 1:28pm
7
no worries cheers for the help.
brndn
June 9, 2008, 1:37pm
8
I think there is no way to evaluate the items and the length in flash and then push them to a new array.
otr at least I dont know the propper method.
brndn
June 9, 2008, 2:59pm
10
var thearray=new Array();
lv = new LoadVars();
lv.onLoad = function() {
st = this.thestring;
count = st.split(",");
c = count.length;
for (z=0; z<c; z++) {
trace(count[z]);
thearray.push(count[z]);
}
LOADTEXT.text = thearray[1];
};
lv.load("/returnarray.php");
works fine