Create Array from dynamic vars


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!

Zaid

Storing objects in the array that way should work? You can also for the array like this:

var myArr = new Array();
myArr[“obj1”] = [“pics/Sunset.jpg”, "thumbs/Sunset.jpg];
myArr[“obj2”] = [“pics/Waterfall.jpg”, "thumbs/Waterfall.jpg];

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?

http://flashrelief.com/thumbgallery/documentation.html#as

If u scroll down a little on that page you’ll see what im constructing the array for …hope this is clear.

And thank you again.
Zaid

Glad to help!

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.

My original post on this subject: http://www.kirupa.com/forum/showthread.php?t=217071

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 :stuck_out_tongue:
//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)

Hows that read to u?

wow.

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

Defective I’ve read ALLOT of your posts man …and if you have the time/effort to put your thoughts down I would love to herer how you’d do this.

This is a learning exercise for me to so teh more methods I can use/play with the better!

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:

var1=willy&var2=jimbob&var3=hooper

and then in the AS you’d do this:

theloadvars.onLoad = function(){
trace (theloadvars.var1) //traces "willy"
trace (theloadvars.var3) //traces "hooper"
}

You see just by using LoadVars and “&” delimiters, you just eliminated all the confusing splits and crap, and also made your code faster:)

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”

But its still not working…any ideas?

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!
?>

Agreed Defective - was just offering him a quicky solution in the beginning to get an array populated w/ a string of name/value pairs.

—> 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?

Sorry i took so long to reply, busy:)

<?
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 'total='.$x.'&pic'.$x.'=gallery/'.$pic.'&thumb'.$x.'=gallery/'.$thumb.'';
$x++;
}
?>

This is what you were getting before:

&img:"gallery/Sunset.jpg",thumb:"gallery/tSunset.jpg"&img:"gallery/Winter.jpg",thumb:"gallery/tWinter.jpg"&img:"gallery/Sunset.jpg",thumb:"gallery/tSunset.jpg"

This is what you’ll get when you use the above php:

total=3&img1=gallery/Sunset.jpg&thumb1=gallery/tSunset.jpg&img2=gallery/Winter.jpg&thumb2=gallery/tWinter.jpg&img3=gallery/Sunset.jpg&thumb3=gallery/tSunset.jpg

Then in flash you could do this:

someloadvars.onLoad = function(){
   trace(someloadvars.total) //traces "3"
   trace(someloadvars.img2) //traces "gallery/Winter.jpg"
   trace(someloadvars.thumb1) //traces "gallery/tSunset.jpg"
}

Make more sense?

For some reason I can seem to load any vars.

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 total r return as undefined?!

AS tags are [ as ] & [ /as ] without spaces:)

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);

AS tags are [ as ] & [ /as ] without spaces:)

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);
}

crawls back down from wall

Man i have no idea what goin on …im still gettin an undefined responce …I even set the php to just echo “totalr=3”

Is it anything to do with my localhost setup?