var myArr = new Array()
//store image paths in objects
var ob1 ={img:"pics/Sunset.jpg",thumb:"pics/Sunset.jpg"}
var ob2 ={img:"pics/Waterfall.jpg",thumb:"pics/Waterfall.jpg"}
//store objects in array
myArr = [ob1, ob2] ;
Hey guys,
I want to load a dynamic amount of vars in to flash then store them into an array. The vars will be structured as above but im not sure how to bundle them in ‘myVar’ when there is a dynamic quantity of them.
Can anyone help? If i need to be clearer please let me know!
Thansk for the reply man, though I dont think ive made myself clear.
The values for each var in the array are going to come from a php script that queries a database.
So the url string returned by the phpcould be var1=1&var2=2 OR var1=1&var2=2&var3=3 and so on …the values and infact the number of variables them selves can change.
Im guessing I need some kind of script that says “for each variable loaded” …is that any clearer?
Aha - I think I understand now. Whenever I’m retreiving from a php file, and if php is returning a string list of variables, I usually delimit them with a comma. So you get this returned, or something similar:
(I just realized you could split the php content on the “&” ampersand character vs. using the comma)
stringfromphp = “var1=1,var2=2,var3=3”,and so on…
then you can set up an array that splits that string on the commas,
(I assume you’ll be retreiving the php data via loadVars in flash?) then you can use a for loop to grab / create objects of that array - I’m not exactly sure how / what content is getting sent back to flash, but maybe this will get you in the right direction:
[AS]
var myArray:Array = new Array();
myArray = stringfromphp.split(",");
var objectArray:Array = new Array();
var len:Number = myArray.length;
for(var i:Number=0; i<len; i++) {
//split the name / value pairs on the “=“
var tempArray:Array = new Array();
tempArray = myArray*.split(”=”);
var obj:Object = new Object();
// assign the value of the var above to a prop in the object
obj = {path:tempArray[1]};
//push the object into the array
objectArray.push(obj);
}
[/AS]
There are a number of different ways to do this - but I think what you’re looking for in general is to be able to loop through all of the content that the php spits back no matter the length / number of items. A for loop based on the length of an array is one way to do this.
Dude your a diamond …thanks so much for putting the time into that.
This is wondering into realms of actions cripting ive yet to approach but with your comments I actually have a good grasp, but I dont under stand what the name of the array containing the vars is …at a guess Im going for objectArray() ? But im not sure if this is right?
I see what you’re doing now - are you using xml or just getting all the data from php?
The first step will be just to generate the correctly formed array from the php data - from there, you can loop through that array to generate objects, and then push them into another array that you’ll then use to create the slide show.
Hey Man,
Im getting the array data from PHP. Originally i was using dynamically written XML but the component im using just wont work with it …the developers are looking into it but im on a timescale so this seemed the next best route in the mean while.
Before i start grappling with the code you’ve knocked up …I was wondering if you’d look at the comments ive added to verify my understanding correct?
var myArray:Array = new Array();
myArray = stringfromphp.split(",");//so this php string recovered and split by commas
var objectArray:Array = new Array();
var len:Number = myArray.length;//this gets the number of vars passed from PHP string
for(var i:Number=0; i<len; i++) {//u’ve lost me on the condition in the for statement
//split the name / value pairs on the “=” -->i get this
var tempArray:Array = new Array();
tempArray = myArray*.split("=");//then store em in a temporary array
var obj:Object = new Object();
// assign the value of the var above to a prop in the object
obj = {path:tempArray[1]};
//push the object into the array
objectArray.push(obj);//im assuming this takes everything that been looped through above and puts it into on array value?
}
With that done I could presumably call the gallery using: ThumbGallery.createFromArray(objectArray)
Sending one comma delimited variable and using splits? I dont want to be a d*ck, but you guys are doing this totally wrong:)
I would have jumped in and told you how to do it correctly, but it looks like the a*s-backword approach worked… and you don’t want to mess with a good thing:D
I would have used “&” as the delimiter in the php outup as that is the delimiter that LoadVars is looking for. Say for example if you were outputting this:
Defective…im not sure if im right here but the splits being used to count the number of vars.
In this case the number of vars is dynamic so by spliting the string they can be counted and loaded in to an array.
I dunno …this may be over my head …i wrote the code with help from you guys below based on using ampersands for splitting:
var my_lv:LoadVars = new LoadVars();
my_lv.load(“flash.php”);
my_lv = gallarray;
var myArray:Array = new Array();
myArray = gallarray.split("&");
var objectArray:Array = new Array();
var len:Number = myArray.length;
for(var i:Number=0; i<len; i++) {
var tempArray:Array = new Array();
tempArray = myArray*.split("=");
var obj:Object = new Object();
obj = {path:tempArray[1]};
objectArray.push(obj);
}
mygallery.createFromArray(objectArray) ;
And the following string outpuit from PHP:
&img:“gallery/Sunset.jpg”,thumb:“gallery/tSunset.jpg”&img:“gallery/Winter.jpg”,thumb:“gallery/tWinter.jpg”&img:“gallery/Sunset.jpg”,thumb:“gallery/tSunset.jpg”
I understand that you are using the splits for a total count, but why not loop the vars into an array and use length? or even better get the count from php and send it as a variable, thats faster:)
post your php and i’ll show you how to send the total. Also, i would send the thumb and full separately with two different variables… I don’t know if you want to tear it apart again, but it would be much more understandable
Thanks man …though this has now left me head spinning!
<?
<?
include (‘functions.php’);
$sql= “SELECT * FROM gallery”;
$query = mysql_query($sql);
$x=1;
while ($result = mysql_fetch_array($query)){
$pic = $result[‘picture’];
$thumb= $result[‘thumbnail’];
echo ‘pic’.$x.’=img:“gallery/’.$pic.’”,thumb:“gallery/’.$thumb.’”&’;
$x++;
}
?>
COuld I not just ad a var to the string with the return value from mysql_num_rows?
Would u be able to to through some AS 2gether so i could see your approach? This is my first time using loops in flash and its confusin hell outa me!
?>
—> creatify dude while i wait for defective to get back i was wondering if you could show me what the string out put for your example would look like from the php?
Wrote a biasic txt output from php that says totalr=3
then the folowing AS (WHATS THE BB CODE FOR AS?)
myVars = new LoadVars();
mydate = new Date();
myVars.load(“http://localhost/the phoenix rose admin/flash.php?”+mydate);
totalR = myVars.totalr;
trace(“http://localhost/the phoenix rose admin/flash.php?”+mydate)
trace(totalr);
and heres the code you wrote with a few corrections:
var myVars:LoadVars = new LoadVars();
var mydate:Date = new Date();
myVars.date = mydate
myVars.sendAndLoad("http://localhost/the phoenix rose admin/flash.php?"+mydate);
totalR = myVars.totalr;
trace("http://localhost/the phoenix rose admin/flash.php", myVars, "POST")
trace(myVars.totalr);
and heres the code you wrote with a few corrections:
var myVars:LoadVars = new LoadVars();
var mydate:Date = new Date();
myVars.date = mydate
myVars.sendAndLoad("http://localhost/the phoenix rose admin/flash.php", myVars, "POST");
myVars.onLoad = function(){
trace(myVars.totalr);
}