View Full Version : php variables imported into flash, but values not assignable
hi,
after using a static string for trying with .split I want to try the same with my php variable. The following Actionscript do I use:
loadVariables("select_2.php", _root);
//array_img is the string which is generated by the PHP script and which
//has to be converted in an array again because Flash cant import PHP arrays
//directly. The strings value is looking like this:
//
//"1,3,directory/pic1.jpg,directory/thumb/pic1.jpg|2,3,directory/pic2.jpg,directory/thumb/pic2.jpg"
//rows_img is the number of images
array_img = array_img.split("|");
for (i=0; i<rows_img; i++) {
trace(array_img[i]);
for (j=0; j<4; j++) {____//j<4 because there are four entries in the second dimension of the array
array_img[i] = array_img[i].split(",");
trace(array_img[i][j]);
}
}
//tf1-4 are vars which are just used to test the output on textfields.
//There are existing four textfields with one var each.
tf1=array_img[0][0];
tf2=array_img[0][1];
tf3=array_img[0][2];
tf4=array_img[0][3];
e.g. after assigning a textfield the array_img var (still the string of the PHP file) I really get the value of the var. But even if I am just assigning the value of array_img to testvar
testvar = array_img;
and testvar is the variable of the textfield there wont be anything.
the trace function wont work too.
it seems that this reason also prevents the execution of the split command.
Does anyone knows an answer?
thanks, ckey
my guess is that your not giving flash enuf time to load the data before trying to do stuff with it. since your using flash5 syntax, i'll show you one way to do it in f5.
first put a blank mc (movieclip) on your stage and give it an instance name of 'loader'
then select the mc 'loader' so that it has a light blue border and open your action panel then put this:
onClipEvent(load){
// load the data when the object is loaded
this.loadVariables("myphp.php");
}
onClipEvent(data){
//run code when data is loaded into object
array_img = array_img.split("|");
for (var i=0; i<rows_img; i++) {
//trace(array_img[i]);
for (var j=0; j<4; j++)
array_img[i] = array_img[i].split(",");
// trace(array_img[i][j]);
}
}
//now if you want to put this data into values on the main timeline do it here.
_parent.tf1=array_img[0][0];
_parent.tf2=array_img[0][1];
_parent.tf3=array_img[0][2];
_parent.tf4=array_img[0][3];
}
anyway... hope this helps.
oh ya... if your using mx?? this will work, but a better way is to use the LoadVars() object. see our tutorials section here.
freddycodes
01-28-2003, 04:58 PM
array_img = array_img.split("|");
for (i=0; i<rows_img; i++) {
trace(array_img[i]);
Where is rows_img set?
the rows_img is also set in the PHP file and counts the images
freddycodes
01-28-2003, 05:51 PM
I think tg nailed it, you need to ensure that the the variables have loaded before you try and use them.
I notice you are loading jpegs into the movie, so you must be using Flash MX, no? If so, there are much easier ways to do this. Like tg said look at the tutorials or use something like this.
myVars = new LoadVars();
myVars.load("select_2.php");
myVars.onLoad = function() {
array_img = myVars.array_img.split("|");
for (i=0; i<rows_img; i++) {
trace(array_img[i]);
for (j=0; j<4; j++) {____//j<4 because there are four entries in the second dimension of the array
array_img[i] = array_img[i].split(",");
trace(array_img[i][j]);
}
}
tf1=array_img[0][0];
tf2=array_img[0][1];
tf3=array_img[0][2];
tf4=array_img[0][3];
};
thanks for your reply. Yes I do use Flash MX and I already changed to code to something similar like u wrote
myLoadVar = new LoadVars();
myLoadVar.load("select_2.php");
myLoadVar.onLoad = function(success) {
testvar = _root.myLoadVar.array_img;
trace("testvar: "+testvar);
trace("array_img: "+_root.myLoadVar.array_img);
test = _root.myLoadVar.array_img;
_root.myLoadVar.array_img = _root.myLoadVar.array_img.split("|");
for (i=0; i<_root.myLoadVar.rows_img; i++) {
trace("main array: "+_root.myLoadVar.array_img[i]);
for (j=0; j<4; j++) {
_root.myLoadVar.array_img[i] = _root.myLoadVar.array_img[i].split(",");
trace("nested array: "+_root.myLoadVar.array_img[i][j]);
}
}
tf1 = _root.myLoadVar.array_img[0][0];
tf2 = _root.myLoadVar.array_img[0][1];
tf3 = _root.myLoadVar.array_img[0][2];
tf4 = _root.myLoadVar.array_img[0][3];
};
if I have a look at the tracing output everything is correct until the nested array. The first nested array value of each main array is shown but the next three values are empty and I dont know why.
ckey
ok, i got it. it was the second .split command which was inside the for loop :) sorry for that.
Thanks to all, ckey
freddycodes
01-28-2003, 07:40 PM
If all you are looking to create in AS is a multi-dimensional array like.
Variable _level0.array_img = [object #81, class 'Array'] [
0:[object #82, class 'Array'] [
0:"1",
1:"3",
2:"directory/pic1.jpg",
3:"directory/thumb/pic1.jpg"
],
1:[object #83, class 'Array'] [
0:"2",
1:"3",
2:"directory/pic2.jpg",
3:"directory/thumb/pic2.jpg"
],
2:[object #84, class 'Array'] [
0:"3",
1:"3",
2:"directory/pic3.jpg",
3:"directory/thumb/pic3.jpg"
],
3:[object #85, class 'Array'] [
0:"4",
1:"3",
2:"directory/pic4.jpg",
3:"directory/thumb/pic4.jpg"
]
]
You can simply just use.
myLoadVar = new LoadVars();
myLoadVar.load("select_2.php");
array_img = [];
myLoadVar.onLoad = function(success) {
tmpArr = this.array_img.split("|");
for (i=0; i<tmpArr.length; i++) {
tmpSubArr = tmpArr[i].split(",");
array_img.push(tmpSubArr);
}
};
the push thing isnt workin correctly but the function works now using another way.
But there is one basic question. How long strings can be which are imported from php? I am thinking of 3000 chars?
freddycodes
01-28-2003, 10:29 PM
It does work, I just tested it. I wrote it that way because you were trying to overwrite a string with an array and I chose to create a new array tmpArr. In order fo you to see it though you would have needed to change
tf1 = _root.myLoadVar.array_img[0][0];
tf2 = _root.myLoadVar.array_img[0][1];
tf3 = _root.myLoadVar.array_img[0][2];
tf4 = _root.myLoadVar.array_img[0][3];
to
tf1 = array_img[0][0];
tf2 = array_img[0][1];
tf3 = array_img[0][2];
tf4 = array_img[0][3];
You see the difference. Before you said it didn't work did you try debugging your movie using Debug > List Variables.
You would have seen the array _root.array_img with all the elements as sub arrays. I guarantee it works and is faster than your first method.
I changed the code like u wrote but it still doesnt work. If u could have a look at the attached file it would be perhaps very helpful. The problem is that the new array is empty when I am sing the push command.
thanks in advance, ckey
freddycodes
01-28-2003, 11:00 PM
You didn't exactly do it like I showed above. Here this is working. Can I see the output that select_2.php outputs. I used a regular text file with this in it.
&array_img=1,3,directory/pic1.jpg,directory/thumb/pic1.jpg|2,3,directory/pic2.jpg,directory/thumb/pic2.jpg|3,3,directory/pic3.jpg,directory/thumb/pic3.jpg|4,3,directory/pic4.jpg,directory/thumb/pic4.jpg
And it worked as advertised.
myLoadVar = new LoadVars();
myLoadVar.load ("select_2.php");
img = [];
myLoadVar.onLoad =function(success) {
tmpArr = this.array_img.split("|");
for(i=0; i<tmpArr.length; i++) {
tmpSubArr = tmpArr[i].split(",");
img.push(tmpSubArr);
}
assignVar();
};
function assignVar() {
trace("final: "+img[0][2]);
tf1 = img[0][0];
tf2 = img[0][1];
tf3 = img[0][2];
tf4 = img[0][3];
};
thanks, it really works :)
but do u know if also strings of 3000 chars will work?
ckey
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.