6 Replies Latest reply on Nov 13, 2006 10:34 AM by exPluda?

    Array writen in flash or php loaded (they're equal but first works and second don't)

    exPluda? Level 1
      Hello,

      I must be crazy, because I can't see any diference between an array diractly writen in actionScript and another one loaded from php to do the same (create empty textfields and add array items to each one)

      can someone explain this to me?

      in as:

      var array_de_familias:Array = new Array("Collection", "Top 10", "News", "Info", "Staff", "Goddies", "Contacts");


      loaded from php

      familias = unescape(gerarFamilias.vars);
      familias.sort();
      familias.splice(0,1);
      var array_de_familias:Array = new Array(familias);

      php output:

      vars="Collection", "Top 10", "News", "Info", "Staff", "Goddies", "Contacts",


      Thanks,
        • 1. Re: Array writen in flash or php loaded (they're equal but first works and second don't)
          Peter Lorent Level 2
          You can't load an object coming from PHP in Flash. You would need to use PHPObject to do that. Or the serializer class by Alessandro Crugnola: http://www.sephiroth.it/test/unserializer/
          • 2. Re: Array writen in flash or php loaded (they're equal but first works and second don't)
            exPluda? Level 1
            Hello LuigiL, how are you?

            but, it is not an object, it is a string, loaded using loadVars
            The only diference I see from what I'm trying to do to what I usually do is that I'm trying to use this string to populate an array...

            var gerarFamilias:LoadVars = new LoadVars();
            gerarFamilias.load("php/gerarFamilias.php");
            gerarFamilias.onLoad = function(sucesso) {
            if (sucesso) {
            familias = unescape(gerarFamilias.vars);
            familias.sort();
            familias.splice(0, 1);
            var array_de_familias:Array = new Array(familias);
            for (var i = 0; i<array_de_familias.length; i++) {
            _root.createEmptyMovieClip("btn_familia"+i, i+1);
            _root["btn_familia"+i].beginFill(0xff7223, 10);
            _root["btn_familia"+i].desenhar_botoes(0, 0, largura, altura);
            _root["btn_familia"+i].endFill();
            _root["btn_familia"+i]._x = 20;
            _root["btn_familia"+i]._y = 10;
            _root["btn_familia"+i].createTextField("txt"+i, i, 0, 0, 90, 15);
            _root["btn_familia"+i]["txt"+i].text = array_de_familias ;
            }
            }
            }
            • 3. Re: Array writen in flash or php loaded (they're equal but first works and second don't)
              Peter Lorent Level 2
              I'm fine thank you. Then I misunderstood you.
              The PHP:
              <?php
              echo "vars=Collection,Top 10,News,Info,Staff,Goddies,Contacts";
              ?>
              In as:
              var gerarFamilias:LoadVars = new LoadVars();
              gerarFamilias.load("php/gerarFamilias.php");
              gerarFamilias.onLoad = function(sucesso) {
              if (sucesso) {
              var array_de_familias:Array = new Array();
              array_de_familias=this.vars.split(",");
              trace(array_de_familias[0]);
              }
              }
              • 4. Re: Array writen in flash or php loaded (they're equal but first works and second don't)
                Level 7

                "Pluda" <webforumsuser@macromedia.com> wrote in message
                news:eja82s$h9t$1@forums.macromedia.com...
                > Hello LuigiL, how are you?
                >
                > but, it is not an object, it is a string, loaded using loadVars
                > The only diference I see from what I'm trying to do to what I usually do
                > is
                > that I'm trying to use this string to populate an array...
                >
                > var gerarFamilias:LoadVars = new LoadVars();
                > gerarFamilias.load("php/gerarFamilias.php");
                > gerarFamilias.onLoad = function(sucesso) {
                > if (sucesso) {
                > familias = unescape(gerarFamilias.vars);
                > familias.sort();
                > familias.splice(0, 1);
                > var array_de_familias:Array = new Array(familias);
                > for (var i = 0; i<array_de_familias.length; i++) {
                > _root.createEmptyMovieClip("btn_familia"+i, i+1);
                > _root["btn_familia"+i].beginFill(0xff7223, 10);
                > _root["btn_familia"+i].desenhar_botoes(0, 0, largura, altura);
                > _root["btn_familia"+i].endFill();
                > _root["btn_familia"+i]._x = 20;
                > _root["btn_familia"+i]._y = 10;
                > _root["btn_familia"+i].createTextField("txt"+i, i, 0, 0, 90, 15);
                > _root["btn_familia"+i]["txt"+i].text = array_de_familias ;
                > }
                > }
                > }
                >

                Pluda,

                You cannot pass native PHP Objects like "Array", "Hash", etc to Flash
                directly. All Flash can get back from your LoadVars is a Flash String. So,
                you need to turn your Flash String into a Flash Array, or a Flash Object.
                To do so, as LuigiL has done already, I would recommend using Sephiroth's
                Serializer class which will take the String and convert it to a native Flash
                Object or Array (depending on what you are returning.)

                You also need to serialize the PHP Object before you return it...
                google "php serialize"

                so, your line:
                familias = unescape(gerarFamilias.vars);
                would look more like:
                mySerialzier = new Serializer();
                familias = mySerializer.unserialize( gerarFamilias.vars);

                Then, familias will be an Array assuming your output from your PHP is
                something like "vars=a:2:{i:0;a:2:{s:11:"description";s:5:"this is my
                description ... etc ... }}"

                Good luck. Once you get the hang of it, it is a piece of cake.



                • 5. Re: Array writen in flash or php loaded (they're equal but first works and second don't)
                  Level 7

                  "LuigiL" <webforumsuser@macromedia.com> wrote in message
                  news:ejaa7v$jtk$1@forums.macromedia.com...
                  > I'm fine thank you. Then I misunderstood you.
                  > The PHP:
                  > <?php
                  > echo "vars=Collection,Top 10,News,Info,Staff,Goddies,Contacts";
                  > ?>
                  > In as:
                  > var gerarFamilias:LoadVars = new LoadVars();
                  > gerarFamilias.load("php/gerarFamilias.php");
                  > gerarFamilias.onLoad = function(sucesso) {
                  > if (sucesso) {
                  > var array_de_familias:Array = new Array();
                  > array_de_familias=this.vars.split(",");
                  > trace(array_de_familias[0]);
                  > }
                  > }
                  >

                  I also meant to mention what LuigiL just beat me to.

                  You can use the split method of the String class to create an Array for what
                  you want.

                  Better to use the answer with the Serializer for much more complex data.


                  • 6. Re: Array writen in flash or php loaded (they're equal but first works and second don't)
                    exPluda? Level 1
                    Hello,

                    thanks you all for reply,

                    I used serializer class a wile ago, but I had some problems with it when using accents, I've talked with Sephy at that time to manage a way of get rid of those errors, but we didn't find a reliable solution for that.

                    In this case, LuigiL solution is what I need, the data I get from php isn't complex, it will just be some names, but serializeclass is indeed a good piece of work.

                    Thanks a ton, I'm going back to work :-)