Groups | Blog | Home
all groups > flash actionscript > august 2007 >

flash actionscript : Flash / php variable passing


chopTheWood
8/8/2007 9:02:30 PM
I have an array in php called $image_array that I want to pass to image_array
in Flash:
the php code is;

-------------------
<?
$dir_name = "/home/myHome/public_html/flashRev7/thumbImages/oil";
$dir = opendir($dir_name);

//READS THE DIRECTORY
while ($file_name = readdir($dir)) {
if (($file_name != ".") && ($file_name != "..")) {

$file_list .= "$file_name";
$file_list .= ",";
}
}


closedir($dir);
// PUTS THE COMMA DELIMITED DIRECTORY INTO AN ARRAY
$imageArray = explode(",",$file_list);



$imageArray[0]="&tempDir="; //THIS IS WORKING FINE, it prints the first comma
delimited string in $imageArray to a Flash dynamic text field called "temp"
with the variable "tempDir".


?>

-------------------------
"tempDir" is the variable name of a dynamic text field in my Flash movie

In Flash I have:
var imageArray:Array =new Array();
imageArray=tempDir; //Here I attempt to make the Flash array "imageArray"
take on the contents of the "tempDir" variable as did the dynamic text field.

Of course it isn't working.. I get "undefined" when I ask Flash to print
"imageArray[0]" to a text field.

--thanks for your help
clbeech
8/8/2007 10:27:52 PM
You should try using the LoadVars class to get the array from the php. I think
something like this would be close.

var imageArray:Array = new Array();
var directory:LoadVar = new LoadVar()

directory.onLoad = function() {
imageArray.slice(directory.imageArray);
}

directory.load("address.php", directory, "GET");
pemcconnell
8/9/2007 12:00:00 AM
Just send the variables via the query string:

i.e. http://www.mysite.com?imagearray=picture.jpg&anotherarrayname=dog.jpg

using php:


header("Location:http://www.mysite.com?imageArray=".$imageArray."&anothervariabl
ename=".$anothervariablename."")

and in your html / php file where you display the flash file, replace the
standard html markup (object) with this javascript

<script language="javascript">
<!--
function QueryString(key)
{
var value = null;
for (var i=0;i<QueryString.keys.length;i++)
{
if (QueryString.keys[i]==key)
{
value = QueryString.values[i];
break;
}
}
return value;
}
QueryString.keys = new Array();
QueryString.values = new Array();

function QueryString_Parse()
{
var query = window.location.search.substring(1);
var pairs = query.split("&");

for (var i=0;i<pairs.length;i++)
{
var pos = pairs[i].indexOf('=');
if (pos >= 0)
{
var argname = pairs[i].substring(0,pos);
var value = pairs[i].substring(pos+1);
QueryString.keys[QueryString.keys.length] = argname;
QueryString.values[QueryString.values.length] = value;
}
}

}
QueryString_Parse();
// write flash obj with query string
function writeFlash() {
var width = '580' // flash dimensions
var height = '820'
var src = 'flashfile.swf' // change to your file location
// queries -- type in the variables you want to send to flash here
var queries =
'?id='+QueryString('imageArray')+'&pagecount='+QueryString('anotherVariableName'
)+''

// assemble flash obj
var l1 = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#
version=8,0,0,0" width="'+width+'" height="'+height+'">;'
var l2 = '<param name="movie" value="'+src+queries+'" />;'
var l3 = '<param name="quality" value="high" />;'
var l4 = '<embed src="'+src+queries+'" quality="high"
pluginspage="http://www.macromedia.com/go/getflashplayer"
type="application/x-shockwave-flash" width="'+width+'"
height="'+height+'"></embed>;'
var l5 = '</object>'

// write all lines
document.write(l1+l2+l3+l4+l5)
}
//-->
</script>

Then you can access the variables in flash by simply typing in the query name

i.e.
imageArray

anothervariablename

Enjoy!

:)

Peter McConnell

--------------------
E.G consulting
pmcconnell@eg-consulting.com
chopTheWood
8/9/2007 12:58:21 AM
I put the code in that you suggested as follows:

var imageArray:Array = new Array();
var directory:LoadVars = new LoadVars();

directory.onLoad = function() {
imageArray.slice(directory.imageArray);
}

directory.load("listfiles.php", directory, "GET");

tempDir="This is imageArray #0"+imageArray[0];

I still get an "undefined" msg.??
tempDir is a dynamic field variable

clbeech
8/9/2007 3:02:37 AM
hmmm .... I will look into it. It just so happens, I'm about to begin working
on a similar php system for a project I have. I will be running some test
tomorrow.

I suspect the php here, what do you get as a result if you run a 'echo' on the
$imageArray varibale in your php?

And try running a trace(directory.imageArray); in the onLoad handler, slice
may not work here, and it may need to be itereated through and assigned. see
if your getting a value passed back to Flash at all.
kglad
8/9/2007 4:00:01 AM
clbeech
8/9/2007 8:32:54 PM
Hey [b]chop[/b], alright I've got this worked out. The array in the php was
not being constructed quite right, and Flash doesn't handle associative arrays
very well, nor does it seem like you can access a php array variable
anyway(somebody got a thought on that?) So to get the values from the
directory names back to Flash we need to create and read a string, just like
you have [i]already[/i] created with '$file_list', we just need to assign it to
a value that Flash can retrieve. So you need to create a 'property' (I used
'files') for the LoadVars to access, and propagate it with the result string,
and 'print' it (or echo), to pass the values, back in Flash you access that
property name, and then use the delimiter there, to propagate the array! (slice
does not work here in this case, because the result is a string, not an array)
Simple as Cake! ;)

[b]pemcconnell[/b], I'm sorry but your method is way overcomplicated, does not
seem to accomplish what he's trying to achieve at all, it does not bring a
directory list [i]back[/i] to Flash, and requires user input?



<?
$dir_name = "/home/myHome/public_html/flashRev7/thumbImages/oil";
$dir = opendir($dir_name);

//READS THE DIRECTORY
while ($file_name = readdir($dir)) {
if (($file_name != ".") && ($file_name != "..")) {
$file_list .= "$file_name";
$file_list .= ",";
}
}

closedir($dir);
print("files=".$file_list);
?>


//::::::::::AND the AS as below:
var imageArray:Array = new Array();
var directory:LoadVars = new LoadVars();

directory.onLoad = function() {
imageArray = directory.files.split(",");
tempDir.text = imageArray.toString();
}

directory.load("listfiles.php", directory, "GET");
GWD
8/9/2007 8:48:03 PM
@clbeech
AMFPHP (PHP version of 'flash remoting') lets you transfer complex objects by
calling server methods from flash and does the translation for you between PHP
associative arrays and native flash objects.

@pemcconnell
You may also want to check out SWFObject (google will get you there). It gives
you a very handy way of embedding via javascript and there are some extra
utilities including a querystring to flashvars transfer if you want to do that
kind of thing.


clbeech
8/9/2007 9:04:32 PM
Again, Cool! [b]GWD[/b], (seems like I'm following you around) I'm going to
check that out, I have a huge, HUGE multidimensional, user generated, dynamic
array, that needs to be stored and retrieved from MySQL, and have just begun
working on the 'save' system. Thanks!
Vee
8/9/2007 9:56:37 PM
clbeech
8/9/2007 10:15:51 PM
Vee
8/9/2007 10:32:08 PM
chopTheWood
8/15/2007 1:16:43 AM
Well thanks for all the help but I still was unclear about how to proceed. I
ended up solving my problem the simplest way I could think of: The following
php returns a comma delimited list of items in a directory directly into a
Flash dynamic field with the variable "theFileList" ("&theFileList=" is
inserted at the beginning of the directory string list in order to do this).
So, I have all the information in a Flash field, now I just have to parse it
(somehow) and put it into an array. The field itself doesn't even have to be
visible. An extra step for sure than just having php hand over an entire array.
I thought it would have been much simpler.
Anyway, thanks for the help and the great links... Anyone finding an a more
elegant procedure, I'd like to hear.


<?
$dir_name = "/home/myFiles/public_html/artText";

$dir = opendir($dir_name);


while ($file_name = readdir($dir)) {
if (($file_name != ".") && ($file_name != "..")) {

$file_list .= "$file_name";
$file_list .= ",";
}
}


closedir($dir);



$theListing="&theFileList=";
$theListing.="$file_list";
echo "$theListing";

?>
clbeech
8/15/2007 1:51:03 AM
[b]chop[/b], look at the code I'd written for you previously. It works just
fine and the AS in the bottom half (everything beyond the "//::::::::::AND the
AS as below: ") will put (parse) the info into an array for you. ace the text
line, that's fine.
chopTheWood
8/15/2007 2:14:42 AM
okay, I'll give it a try... I was just experimenting with the split method for
creating an array from the text field value. That worked but your might
eliminate the need for the text field to hold the string....
clbeech
8/15/2007 2:38:23 AM
chopTheWood
8/15/2007 2:56:53 AM
Yup, okay, you win. THe AS array actually got populated. I'll wait until tomorrow to figure out why it works (when I'm more awake). Pretty simple to boot.
AddThis Social Bookmark Button