10 Replies Latest reply on Jul 28, 2007 8:37 PM by _name

    dynamic insertion

    _name Level 1
      hello all,

      am new and thankful to be a part of this forum.

      does any one here know if it is possible to retrieve a list of jpeg images from a folder onLoad and push them into an array? my goal is to allow my client to insert as many or little jpegs as wished into an external folder and when his site loads retrieve them and organize them in an array.

      thanks,
      _name

        • 1. Re: dynamic insertion
          _name Level 1
          should have mentioned this in the above post: am using mx 04 v7.0 as2.
          thank you for any advice,
          _name
          • 2. dynamic insertion
            Greg Dove Level 4
            Welcome to the actionscript forum.

            In answer to your question:
            The short version:Flash can't read a directory itself - it needs to have something else to give it the data.
            More detail: You could have a server script read the directory and pass the data to flash in a number of different ways (sounds like this might make the most sense)... or you could manually update the list of files in an xml or text file each time they're changed - flash could load this same file each time and check it and convert it into an array of image urls (and/or other related data about each image - probably xml is best for doing this).

            Of course, once flash has the urls for each of the images, it can then loop through and load them. And then you can easily have an array where each element references the movieclip that is the image you have loaded.


            • 3. Re: dynamic insertion
              Level 7
              You'll need a server-side script for this. Here's a PHP function that I use,
              which is part of a larger utility class:

              //returns an array of files in the given folder
              //requires a relative path to the folder. Like: ../../images/links/
              function getFiles($dir)
              {
              $dirArray = array();
              if ($handle = opendir($dir)) {
              while (false !== ($file = readdir($handle))) {
              if ($file != "." && $file != "..") {
              if(!is_dir($dir.$file)) {
              //don't add .php files to the list
              if(strpos($file, ".php") === false){
              array_push($dirArray, array("label" => basename($file), "data" =>
              basename($file)));
              }
              }
              }
              }
              closedir($handle);
              }
              return($dirArray);
              }


              Unless you want others to see your data, outputting from here to XML would
              just add a unnecessary level of complexity.

              I call this using remoting - AMFPHP. Notice too, I'm building an array with
              label and data properties - so the return can be the dataProvider for a
              comboBox. Populating the CB with a list of files from the server is as easy
              as:

              var pc:PendingCall = utilityService.getFiles(whichImageFolder);
              pc.responder = new RelayResponder(this, "handleGetImagesResult",
              "handleGetImagesError");

              function handleGetImagesResult(re:ResultEvent) {
              fileList.dataProvider = re.result;
              }

              fileList being the instance name of the comboBox. Get remoting working and
              it'll make life so much nicer when dealing with this kind of stuff.

              --
              Dave -
              Head Developer
              http://www.blurredistinction.com
              Adobe Community Expert
              http://www.adobe.com/communities/experts/


              • 4. Re: dynamic insertion
                Greg Dove Level 4
                "Get remoting working and it'll make life so much nicer when dealing with this kind of stuff."

                @Dave: I couldn't agree more. Are you using this C extension for PHP for amf encoding/decoding support?
                http://www.5etdemi.com/blog/archives/2007/01/amfphp-19-beta-2-ridiculously-faster

                - GWD
                • 5. Re: dynamic insertion
                  Level 7
                  >> @Dave: I couldn't agree more. Are you using this C extension for PHP for
                  >> amf
                  encoding/decoding support?

                  No, but I had a peek and it sure looks nice. Fast is good... I'm gonna try
                  and get it installed for my current project.

                  --
                  Dave -
                  Head Developer
                  http://www.blurredistinction.com
                  Adobe Community Expert
                  http://www.adobe.com/communities/experts/


                  • 6. Re: dynamic insertion
                    _name Level 1
                    hello,

                    thank you all for your suggestions.
                    here is the solution i came up with:

                    floorPics_array = new Array();
                    floorNames_array = new Array();
                    externalData = new LoadVars();

                    externalData.onLoad = function() {
                    floorPics_array = externalData.externalFloorPics.split(",");
                    floorNames_array = externalData.externalFloorNames.split(",");
                    var spacing:Number = 60;
                    for (i = 0; i < floorPics_array.length; i++) {
                    var name:String = "floorDisplay" + i;
                    var y:Number = i * spacing;
                    loaderClip.attachMovie("floorDisplay", name, i);
                    loaderClip[name].floorSample.loadMovie(floorPics_array );
                    loaderClip[name].floorName.text = floorNames_array
                    ;
                    loaderClip[name]._y = y;
                    }
                    };

                    externalData.load("EXTERNAL_DATA/EXTERNAL_DATA.txt");


                    i used an external txt file and an internal mc for attaching and loading.

                    _name
                    • 7. Re: dynamic insertion
                      _name Level 1
                      am new to actionscript and flash, and don't really know about xml.
                      how could i use xml instead of a txt file?
                      is it more efficient to use xml? if so, why?
                      • 8. Re: dynamic insertion
                        Greg Dove Level 4
                        XML vs. plain text files
                        I can respond with my views. Nothing official here though... just my thoughts for what they're worth.

                        To start with you're really dealing with ways of representing data in plain text here. XML is just a commonly recognised way of doing that, and it is able to describe the hierarchical nature of the data.
                        That is probably the 'efficiency' part of it, because in general its less efficient without compression in terms of the size taken to represent the data. So its the API support for parsing the XML text representation into internal representations of the data in different programming languages that is 'efficient', and the ease of extending code (once you get used to it) to reflect changes in the data structure compared to having your own customised approach to representing data.
                        It is also not too difficult for 'humans' to interpret and maintain.

                        Plain text in flash gives you 2 options using LoadVars like you have used.
                        You have used the urlencoded name/value pairs option (which is really what most people would use LoadVars for).
                        Its also possible in the onData handler for a LoadVars to implement your own parsing routine,
                        so it really could be in any format you like. I've answered requests here for people
                        who wanted it in
                        [HEADLINE]
                        A headline
                        [BODY]
                        This is the body copy
                        [keywords]
                        some keywords here
                        [HEADLINE]

                        ..etc
                        So that's also possible.

                        Personally I prefer to use xml if I can because of the reasons mentioned earlier. Although where its
                        very simple and you can be certain requirements won't change you can use other ways for other reasons.
                        E.g the people wanting to do it as per the example above were asking on the basis
                        of client request - it was what the client wanted in order to have the confidence to maintain things by hand.

                        Hope that helps. Take a look at the code
                        -GWD

                        • 9. Re: dynamic insertion
                          _name Level 1
                          thank you, gwd.
                          • 10. dynamic insertion
                            _name Level 1
                            post no longer needed