# Create Array from dynamic vars

**URL:** <https://forum.kirupa.com/t/create-array-from-dynamic-vars/186769>\
**Category:** flash\
**Created:** [April 24, 2006, 2:47pm UTC](https://forum.kirupa.com/t/create-array-from-dynamic-vars/186769 "2006-04-24T14:47:04Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zaid\_W1red](https://avatars.discourse-cdn.com/v4/letter/z/cab0a1/32.png) [@Zaid\_W1red](https://forum.kirupa.com/u/Zaid_W1red)\
**Post date:** [April 24, 2006, 2:47pm UTC](https://forum.kirupa.com/t/create-array-from-dynamic-vars/186769/1 "2006-04-24T14:47:04Z")

</div>

```php

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

---

<div class="post-metadata">

**Author:** ![creatify](https://avatars.discourse-cdn.com/v4/letter/c/d07c76/32.png) [@creatify](https://forum.kirupa.com/u/creatify)\
**Post date:** [April 24, 2006, 3:28pm UTC](https://forum.kirupa.com/t/create-array-from-dynamic-vars/186769/2 "2006-04-24T15:28:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Zaid\_W1red](https://avatars.discourse-cdn.com/v4/letter/z/cab0a1/32.png) [@Zaid\_W1red](https://forum.kirupa.com/u/Zaid_W1red)\
**Post date:** [April 24, 2006, 3:32pm UTC](https://forum.kirupa.com/t/create-array-from-dynamic-vars/186769/3 "2006-04-24T15:32:18Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![creatify](https://avatars.discourse-cdn.com/v4/letter/c/d07c76/32.png) [@creatify](https://forum.kirupa.com/u/creatify)\
**Post date:** [April 24, 2006, 3:55pm UTC](https://forum.kirupa.com/t/create-array-from-dynamic-vars/186769/4 "2006-04-24T15:55:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Zaid\_W1red](https://avatars.discourse-cdn.com/v4/letter/z/cab0a1/32.png) [@Zaid\_W1red](https://forum.kirupa.com/u/Zaid_W1red)\
**Post date:** [April 24, 2006, 4:03pm UTC](https://forum.kirupa.com/t/create-array-from-dynamic-vars/186769/5 "2006-04-24T16:03:56Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![creatify](https://avatars.discourse-cdn.com/v4/letter/c/d07c76/32.png) [@creatify](https://forum.kirupa.com/u/creatify)\
**Post date:** [April 24, 2006, 4:19pm UTC](https://forum.kirupa.com/t/create-array-from-dynamic-vars/186769/6 "2006-04-24T16:19:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Zaid\_W1red](https://avatars.discourse-cdn.com/v4/letter/z/cab0a1/32.png) [@Zaid\_W1red](https://forum.kirupa.com/u/Zaid_W1red)\
**Post date:** [April 24, 2006, 6:09pm UTC](https://forum.kirupa.com/t/create-array-from-dynamic-vars/186769/7 "2006-04-24T18:09:29Z")

</div>

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](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 😛  
//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?

---

<div class="post-metadata">

**Author:** ![JoshuaJonah](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/joshuajonah/32/1775_2.png) [@JoshuaJonah](https://forum.kirupa.com/u/JoshuaJonah)\
**Post date:** [April 24, 2006, 6:20pm UTC](https://forum.kirupa.com/t/create-array-from-dynamic-vars/186769/8 "2006-04-24T18:20:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Zaid\_W1red](https://avatars.discourse-cdn.com/v4/letter/z/cab0a1/32.png) [@Zaid\_W1red](https://forum.kirupa.com/u/Zaid_W1red)\
**Post date:** [April 24, 2006, 6:38pm UTC](https://forum.kirupa.com/t/create-array-from-dynamic-vars/186769/9 "2006-04-24T18:38:37Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![JoshuaJonah](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/joshuajonah/32/1775_2.png) [@JoshuaJonah](https://forum.kirupa.com/u/JoshuaJonah)\
**Post date:** [April 24, 2006, 6:50pm UTC](https://forum.kirupa.com/t/create-array-from-dynamic-vars/186769/10 "2006-04-24T18:50:19Z")

</div>

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:

```auto
var1=willy&var2=jimbob&var3=hooper

```

and then in the AS you’d do this:

```auto
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:)

---

<div class="post-metadata">

**Author:** ![Zaid\_W1red](https://avatars.discourse-cdn.com/v4/letter/z/cab0a1/32.png) [@Zaid\_W1red](https://forum.kirupa.com/u/Zaid_W1red)\
**Post date:** [April 24, 2006, 6:55pm UTC](https://forum.kirupa.com/t/create-array-from-dynamic-vars/186769/11 "2006-04-24T18:55:56Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![JoshuaJonah](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/joshuajonah/32/1775_2.png) [@JoshuaJonah](https://forum.kirupa.com/u/JoshuaJonah)\
**Post date:** [April 24, 2006, 6:59pm UTC](https://forum.kirupa.com/t/create-array-from-dynamic-vars/186769/12 "2006-04-24T18:59:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Zaid\_W1red](https://avatars.discourse-cdn.com/v4/letter/z/cab0a1/32.png) [@Zaid\_W1red](https://forum.kirupa.com/u/Zaid_W1red)\
**Post date:** [April 24, 2006, 7:04pm UTC](https://forum.kirupa.com/t/create-array-from-dynamic-vars/186769/13 "2006-04-24T19:04:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![creatify](https://avatars.discourse-cdn.com/v4/letter/c/d07c76/32.png) [@creatify](https://forum.kirupa.com/u/creatify)\
**Post date:** [April 24, 2006, 7:38pm UTC](https://forum.kirupa.com/t/create-array-from-dynamic-vars/186769/14 "2006-04-24T19:38:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Zaid\_W1red](https://avatars.discourse-cdn.com/v4/letter/z/cab0a1/32.png) [@Zaid\_W1red](https://forum.kirupa.com/u/Zaid_W1red)\
**Post date:** [April 24, 2006, 7:42pm UTC](https://forum.kirupa.com/t/create-array-from-dynamic-vars/186769/15 "2006-04-24T19:42:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JoshuaJonah](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/joshuajonah/32/1775_2.png) [@JoshuaJonah](https://forum.kirupa.com/u/JoshuaJonah)\
**Post date:** [April 24, 2006, 7:46pm UTC](https://forum.kirupa.com/t/create-array-from-dynamic-vars/186769/16 "2006-04-24T19:46:06Z")

</div>

Sorry i took so long to reply, busy:)

```php
<?
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:

```auto
&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:

```auto
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:

```auto
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?

---

<div class="post-metadata">

**Author:** ![Zaid\_W1red](https://avatars.discourse-cdn.com/v4/letter/z/cab0a1/32.png) [@Zaid\_W1red](https://forum.kirupa.com/u/Zaid_W1red)\
**Post date:** [April 24, 2006, 8:09pm UTC](https://forum.kirupa.com/t/create-array-from-dynamic-vars/186769/17 "2006-04-24T20:09:41Z")

</div>

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](http://localhost/the) phoenix rose admin/flash.php?”+mydate);  
totalR = myVars.totalr;  
trace(“[http://localhost/the](http://localhost/the) phoenix rose admin/flash.php?”+mydate)  
trace(totalr);

and total r return as undefined?!

---

<div class="post-metadata">

**Author:** ![JoshuaJonah](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/joshuajonah/32/1775_2.png) [@JoshuaJonah](https://forum.kirupa.com/u/JoshuaJonah)\
**Post date:** [April 24, 2006, 8:24pm UTC](https://forum.kirupa.com/t/create-array-from-dynamic-vars/186769/18 "2006-04-24T20:24:40Z")

</div>

> [@Zaid\_W1red](#):
>
> 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](http://localhost/the) phoenix rose admin/flash.php?”+mydate);  
> totalR = myVars.totalr;  
> trace(“[http://localhost/the](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:

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

```

---

<div class="post-metadata">

**Author:** ![JoshuaJonah](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/joshuajonah/32/1775_2.png) [@JoshuaJonah](https://forum.kirupa.com/u/JoshuaJonah)\
**Post date:** [April 24, 2006, 8:25pm UTC](https://forum.kirupa.com/t/create-array-from-dynamic-vars/186769/19 "2006-04-24T20:25:47Z")

</div>

> [@Zaid\_W1red](#):
>
> 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](http://localhost/the) phoenix rose admin/flash.php?”+mydate);  
> totalR = myVars.totalr;  
> trace(“[http://localhost/the](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:

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

```

---

<div class="post-metadata">

**Author:** ![Zaid\_W1red](https://avatars.discourse-cdn.com/v4/letter/z/cab0a1/32.png) [@Zaid\_W1red](https://forum.kirupa.com/u/Zaid_W1red)\
**Post date:** [April 24, 2006, 8:41pm UTC](https://forum.kirupa.com/t/create-array-from-dynamic-vars/186769/20 "2006-04-24T20:41:02Z")

</div>

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

[Next page](https://forum.kirupa.com/t/create-array-from-dynamic-vars/186769.md?page=2)
