16 Replies Latest reply on Aug 14, 2007 7:56 PM by chopTheWood

    Flash / php variable passing

    chopTheWood
      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
        • 1. Flash / php variable passing
          clbeech Level 3
          You should try using the LoadVars class to get the array from the php. I think something like this would be close.
          • 2. Re: Flash / php variable passing
            chopTheWood Level 1
            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
            • 3. Re: Flash / php variable passing
              clbeech Level 3
              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.
              • 4. Re: Flash / php variable passing
                kglad Adobe Community Professional & MVP
                you can't access imageArray[0] before your onLoad method executes.
                • 5. Re: Flash / php variable passing
                  pemcconnell Level 1
                  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."&anothervariablename=".$anothervariablenam e."")

                  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 ==key)
                  {
                  value = QueryString.values
                  ;
                  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 .indexOf('=');
                  if (pos >= 0)
                  {
                  var argname = pairs
                  .substring(0,pos);
                  var value = pairs .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
                  • 6. Re: Flash / php variable passing
                    clbeech Level 3
                    Hey chop, 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 already 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! ;)

                    pemcconnell, 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 back to Flash, and requires user input?

                    • 7. Re: Flash / php variable passing
                      Greg Dove Level 4
                      @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.

                      • 8. Re: Flash / php variable passing
                        clbeech Level 3
                        Again, Cool! GWD, (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!
                        • 9. Flash / php variable passing
                          >Vee<
                          try this source too. they have some tutorials for integrating external data into flash as2 and as3. look to the menu on the left:
                          http://www.flash-db.com/Tutorials/loadingAS3/loadingData.php?page=3
                          • 10. Re: Flash / php variable passing
                            clbeech Level 3
                            Great Vee, Thanks!
                            • 11. Re: Flash / php variable passing
                              >Vee< Level 1
                              no problem. i'd be interested when you get it up and running to see the finalized code.
                              • 12. Re: Flash / php variable passing
                                chopTheWood Level 1
                                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";

                                ?>
                                • 13. Re: Flash / php variable passing
                                  clbeech Level 3
                                  chop, 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.
                                  • 14. Re: Flash / php variable passing
                                    chopTheWood Level 1
                                    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....
                                    • 15. Re: Flash / php variable passing
                                      clbeech Level 3
                                      right on, let us know how it goes.
                                      • 16. Re: Flash / php variable passing
                                        chopTheWood Level 1
                                        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.
                                        thanks