18 Replies Latest reply on Jul 16, 2009 9:34 AM by flexgoes

    Flex equivalent of $_GET['n']

    andylyon87

      Hi Guys

       

      I have been getting hacked off trying to find the answer to this q. I would think its pretty simple!

       

      My app is simple. It shows photos of my work, these are gained from a database using an XML page.

       

      The problem is that I need a way of passing the URL variable "n" which is an int into the xml doc!

       

      my mysql query stands at which works :

       

      $result = mysql_query("SELECT imageId FROM imgLink WHERE jobId=2");

       

      I had it at, but this is when my problem occured, this is essentially what I want:

       

      $result = mysql_query("SELECT imageId FROM imgLink WHERE jobId='".$_GET['n']."'");

       

      I have had a look at passing it from the flex instance to the XML file when it is called but cannot work out how!

       

      PLEASE PLEASE HELP IM AT THE END OF MY TETHER!!!

        • 1. Re: Flex equivalent of $_GET['n']
          Tpae

          Hello,

           

          I would try using HTTPService.

           

          var connector:HTTPService;
          
          public function getStuff(passingVariable:String):void {
          
               connector = new HTTPService();
               connector.url = "http://sample.link";
               connector.method = "GET";
          
               var param:Object = new Object;
               param.variable = passingVariable;
               param.variableTwo = passingVariable + "hello";
               param.variableThree = passingVariable + "hello2";
          
               connector.send(param);
          }
          

           

          You can pass in parameters through $_GET, and you can retrieve them like this:

           

          $_GET['variable'];
          $_GET['variableTwo'];
          $_GET['variableThree'];
          

           

          You can retrieve the XML output in Flex like this:

           

          connector.lastResult.node1;
          

           

          I hope this helps!

          • 3. Re: Flex equivalent of $_GET['n']
            andylyon87 Level 1

            I am really sorry guys but you are going to have to treat me like a complete newbie, I normally pick up coding dead quick. I am having huge problems getting my head around flex!

             

            I don't have a clue where to put any of the code shown, all the examples shown are how to pass a variable using GET request method on the clik of a button!

             

            I thought I may aswell include my mxml source (its small!)

             

            <?xml version="1.0" encoding="utf-8"?>

            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" applicationComplete="service.send()" height="570" width="610" backgroundGradientAlphas="[1.0, 1.0]" backgroundGradientColors="[#222222, #222222]">


                <mx:HTTPService id="service" url="photos.php" resultFormat="e4x" />

             

             

            <mx:HBox width="559" height="511" horizontalGap="6" horizontalAlign="center">

             

            <mx:VBox height="510" width="542" horizontalAlign="center" cornerRadius="0">

             

            <mx:Image id="image"

            source="{photosList.selectedItem.@image}"

            horizontalAlign="center"

            open="progressBar.visible = true"

            complete="progressBar.visible = false"

            themeColor="#838383" width="522" height="348" autoLoad="true" hideEffect="fade" showEffect="fade" creationCompleteEffect="fade"/>

             

             

            <mx:ProgressBar id="progressBar"

               source="{image}"

                    visible="false"

               color="#FFFFFF" />

             

            <mx:HorizontalList dataProvider="{service.lastResult.photo}" id="photosList" change="photosList.selectedIndex" width="411" height="112" selectedIndex="1">

             

            <mx:itemRenderer>

             

                    <mx:Component>

                    <mx:Image

                    horizontalAlign="center"

                    source="{data.@thumb}"

                    toolTip="{data.@title}"

                    width="137"

                    height="91"

                    />

                    </mx:Component>

             

                    </mx:itemRenderer>

             

            </mx:HorizontalList>

             

            </mx:VBox>

                   

                </mx:HBox>       

            <mx:Style source="style.css"/>

             

               

            </mx:Application>

             

            Where in that would I put something to capture a variable from the URL and pass it to photos.php (which returns and XML doc)

             

            Im sorry guys I just don't follow it at all!

            • 4. Re: Flex equivalent of $_GET['n']
              flexgoes

              If I understand you correctly, you want to get all the images right away?

              To put function and actionscript code you need to declare a block like this. Just an example

                  <mx:Script>
                  <![CDATA[
                  var connector:HTTPService;

                  public function getStuff(passingVariable:String):void {

                   connector = new HTTPService();
                   connector.url = "http://sample.link";
                   connector.method = "GET";

                   var param:Object = new Object;
                   param.variable = passingVariable;
                   param.variableTwo = passingVariable + "hello";
                   param.variableThree = passingVariable + "hello2";

                   connector.send(param);
                 }
                  ]]>
                  </mx:Script>

              You can put it after the application tag on top. Then instead of doing service.send() on applicationComplete. Type in a function name like init()

              or getStuff. It will get called after your application starts and then Tpae's code will do the rest.

              • 5. Re: Flex equivalent of $_GET['n']
                andylyon87 Level 1

                So just to double check if I was to say go to gallery.php?n=1 this script would read the n value into the script?

                 

                All I need to do is to get 'n' from the URL and pass it to the XML file that then uses it to get info from the database. I just can't get my head around it!

                 

                Thank you for all your advice I am just not getting this at all!

                • 6. Re: Flex equivalent of $_GET['n']
                  flexgoes Level 1

                  The above code allow you to send information to php. Then php can process the query based on what n you pass to it. I guess the question to be asked is how does the xml file fit into the process?

                  It almost looks like you are trying to pass variables to the flex application and not php.

                  • 7. Re: Flex equivalent of $_GET['n']
                    andylyon87 Level 1

                    Yeh I suppose it must be :s !

                     

                    how would I reference the passed variable in a php query?

                    • 8. Re: Flex equivalent of $_GET['n']
                      flexgoes Level 1

                      Ok, I did some testing and it seems you can just use $_GET['varname'] to obtain the values in php.


                      The names are relative to how you name them in the object so for example if you had an app like this.

                       

                      <?xml version="1.0" encoding="utf-8"?>
                      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" backgroundGradientColors="[#FFFFFF, #888888]" creationComplete="getStuff()">
                          <mx:Script>
                          <![CDATA[
                          import mx.rpc.*;
                          import mx.rpc.events.ResultEvent;
                          import mx.rpc.events.FaultEvent;

                       

                          public function getStuff():void {
                          
                           var param:Object = new Object;
                           param.var1 = "name1";
                           param.var2 =  "hello";
                           param.var3 =  "hello2";

                       

                             request.send(param);
                           }
                         
                          private function showdata(evt:ResultEvent):void {
                            //test.text = evt.result.toString();

                            //do processing of returned data here;
                          }
                         
                          private function faultHandler(evt:FaultEvent):void {
                            test.text = evt.fault.message;
                          }
                         
                          ]]>
                          </mx:Script>
                          <mx:TextArea id="debug" width="50%" height="50%"/>
                          <mx:HTTPService id="request" url="yourphp URL" useProxy="false" method="GET" result="showdata(event)" fault="faultHandler(event)"/>
                      </mx:Application>

                       

                      Then you can reference the values in php using $_GET['var1'] and so on. Note if you don't put the compiled swf and the php script under the same domain. There will be a security error. It behaves fine once you upload everything onto the web but not locally. I ran into this recently. There are ways to get around it in the forums but not really a concern.

                      • 9. Re: Flex equivalent of $_GET['n']
                        andylyon87 Level 1

                        Thanks I am now following how the parameters get fed from mxml to xml, BUT I still don't see how the variable is grabbed from the URL and used in this?

                        • 10. Re: Flex equivalent of $_GET['n']
                          flexgoes Level 1

                          In that code its assuming you know what parameters to pass. If you want to reference those particular values in mxml, then you can declare the object variable globally here:

                          ....
                              import mx.rpc.events.FaultEvent;

                           

                              private  var param:Object;

                           

                              public function getStuff():void {

                                  param = new Object;

                                 //rest is same

                              }

                          ....

                          That will allow you to use it in any functions in the script block, just call param.value  If you want to use that value in a mxml control, then use the bindable property and reference it in mxml like this. I hope that answers your question and I didn't understand you wrong.

                          [Bindable]

                          private var param:Object

                          <mx:TextArea text="{param.var1}" />

                          • 11. Re: Flex equivalent of $_GET['n']
                            Tpae Level 1

                            So there are 2 steps to this.

                             

                            When you instantiate HTTPService object, you can pass in a parameter object.

                             

                            Parameter object can be seen as follows:

                            var parameters:Object = new Object;
                            parameters.variableToSend = "testing!";
                            

                             

                            Now, when you send that variableToSend to PHP, you can send it through the send(param) method, as demonstrated below:

                            var newService:HTTPService = new HTTPService();
                            
                            // set method (post or get)
                            
                            newService.method = "GET";
                            newService.url = "URL LINK TO PHP FILE";
                            
                            // the parameters we created above
                            newService.send(parameters);
                            
                            

                             

                            Now, from the URL link, you can access your variables like this:

                            echo $_GET['variableToSend'];
                            // it should display 'testing!'
                            

                             

                            Please note that this like using a form in HTML. Set the method to POST or GET, and retrieve your items through $_GET or $_POST

                             

                             

                            Now, once the variable is RETURNED, (in parsed XML format), you can access it like this:

                            <node>
                                 <innerNode>Hi</innerNode>
                            </node>
                            

                             

                            newService.lastResult.node.innerNode should give you msg "Hi"

                            hope that helps!

                            • 12. Re: Flex equivalent of $_GET['n']
                              andylyon87 Level 1

                              It is getting closer.

                               

                              The point is none of the parameters are predetermined! It is going to be in the URL.

                               

                              The code above shows how to pass a param , using GET, that is setup in the flex mxml doc.

                               

                              I need to GET the URL and then pass part of it as a param to the XML.

                              • 13. Re: Flex equivalent of $_GET['n']
                                Tpae Level 1

                                I am having trouble figuring out what you need to accomplish. Did you mean to parse the variable called back from xml?

                                 

                                I seem to recall that you want to use the variable $_GET['n'], where n is a parameter passed from flex.

                                 

                                $_GET['n'] is then parsed in to XML, which returns back to flex.

                                 

                                am I wrong?

                                • 14. Re: Flex equivalent of $_GET['n']
                                  andylyon87 Level 1

                                  To give you an idea of what I mean http://www.abdulqabiz.com/files/test/QueryStringSample.html?myName=Abdul&myHomeTown=Bangal ore

                                   

                                  This guy has the url params fed from the URL into the flex, I then need these params to be read by the XML it is that simple (or so I thought). I just have no idea how to do it!

                                  • 15. Re: Flex equivalent of $_GET['n']
                                    Plstsn Level 3

                                    Hi.

                                    I'm newbie at Flex, but try taking a peep at Lynda.com, thay have some great basics about the HTTPService:

                                    http://www.lynda.com/home/DisplayCourse.aspx?lpk2=438

                                     

                                    - and scroll down to chapter 11.

                                    • 16. Re: Flex equivalent of $_GET['n']
                                      andylyon87 Level 1

                                      have heard lynda.com is meant to be good. Thanks for the pointer.

                                       

                                      I honestly thought this would be a quick post ha ha, I remember reading somewhere that flex was a piece of cake I think I might hunt that person down lol.

                                       

                                      Still looking for help guys I am soooooo close I can feel it. I would like to say thanks for all the help so far too its been great learning.

                                      • 18. Re: Flex equivalent of $_GET['n']
                                        flexgoes Level 1

                                        Ok, I read the source code and I think I know how its done. That code uses the actionscript external interface library to call javascript functions. Its written for older actionscript but you can still look at it  http://www.abdulqabiz.com and search for querystring. That demo is an html page with embedded swf using swfobject.

                                        The two most important lines are here:

                                           _all:String =  ExternalInterface.call("window.location.href.toString");
                                           _queryString:String = ExternalInterface.call("window.location.search.substring", 1);

                                        if you look at this page https://developer.mozilla.org/en/DOM:window.location, it tells you the first line grabs the entire URL and second grabs everything after the question mark. I had no idea you could do this so I learned something too.

                                        To use external interface, you need to place an import statement for the library.        import flash.external.*;     Then, you can call the functions like two lines above. Usually the function needs to be defined in javascript but not here. You might wanna test this in all the browsers also as this sort of call may or may not work for all browsers.

                                         

                                        Parsing the string is then just a matter of using substring and spliting the string or use regular expressions. Its common amongst many languages. After that, I think you can then use the previous code to pass the values to PHP.

                                         

                                        UPDATE: It seems a few posts recently have been asking the same topic. I saw another way to do it using browser manager, probably more reliable. Link is here: http://oreilly.com/pub/a/flex/excerpts/flex3-cookbook/chapter-20.html?page=2

                                         

                                        All flex related Adobe docs (including language reference) can be found and downloaded here: http://www.adobe.com/support/documentation/en/flex/