12 Replies Latest reply on Apr 16, 2008 7:02 PM by kglad

    Reiterative (dynamic)  Loadvars

    akiva.kent Level 1
      It appears that once a new LoadVars object is used it can't be reused to send POST data again.

      For example, I have 40 questions in a test.

      Each time the student answers a question, the information uses SendAndLoad via POST (see below) to insert a new mySQL record of the data from the question.

      The test loops through the same routine 40 times and therefore should insert 40 unique records representing the student's answer for each question.

      I can't create a _global or array loadvars object and then reassign the array elements within each question (which would be a array element or the reused _global object, in another frame. Flash won't let me do it.

      Any insights? An adobe forum search and google search haven't turned up anything yet other than this routine below (thread is also below in topic: dynamic loadvars)

      for(i = 0; i < maxnumbersinlist; i++) {
      _root["show" + i] = myLoadVar["display" + i];
      }

      Please explain how this this would work, would it be? I can't conceive of a send_lv[ques1], send_lv[ques2], etc.

      Here's the website in case you need to understand what I'm attempting to do:
      http://www.4themax.com/education/regents_quest/quiz-home.html

      With the example below, I want to re-use send_lv and result_lv so

      Question 1: send_lv.nme = "John";

      Question 2: send_lv.nme = "testing server 234";

      Question 3: send_lv.nme = "3"; etc.

      (the data is fictitious, but the data will be different each time the send_lv is sent via POST to a php file to insert the mySQL data)

      Otherwise, I'd have to create 40 different loadvars objects, i.e. send_lv1.nme for question 1, send_lv2.nme for question 2, etc. which is very cumbersome.


      ------- thread from previous post -----------------
      Here's WHAT WORKS! THANKS kglad - I've also put a kudo to you on my splash screen.
      Hint: I had to make the php file write the contents of the variable to a file because it doesn'tdisplay on the screen with echo in order to see that the variable was passed from flash to php.

      Adobe document source: http://livedocs.adobe.com/flashlite/2/main/00000572.html

      //var submitListener:Object = new Object();
      //submitListener.click = function(evt:Object) {
      var result_lv:LoadVars = new LoadVars();
      /*
      result_lv.onLoad = function(success:Boolean) {

      if (success) {
      result_ta.text = " sent successfully!";
      } else {
      result_ta.text = "Error connecting to server.";
      }

      };
      */
      var send_lv:LoadVars = new LoadVars();
      send_lv.nme = "Testing server2";
      sending.text = send_lv.nme;
      send_lv.sendAndLoad("loadvars.php", result_lv, "POST");
      //};
      //submit_button.addEventListener("click", submitListener);

      <?php
      $contents = $_POST['nme'];
      echo $contents; //DOESN'T DISPLAY CONTENTS
      //open the file in write only mode - used to see the contents of the _POST variable sent by LoadVars
      $file = fopen('loadvars.txt','w');

      //write the contents
      fwrite($file, $contents);

      //close the file
      fclose($file);

      ?>
      ---------------------------------------------------------- thread from forum is below: ------------------
      arfa
      User is offline
      View Profile

      Junior Member Posts: 3
      Joined: 03/25/2003
      01/26/2008 01:01:02 AM
      Reply | Quote | Top | Bottom

      HI All,

      I am writing actionscript to get variables from a database using php. I have used the LoadVars object and can set a variable using the following line of code which works

      _root.show1=myLoadVar.display1

      In this case myLoadVar is an instance of the Loadvars class and display1 is one of a series of variables sent from php.

      I need to make the right hand side of the expression dynamic eg(display1, display2, display3 etc)

      I have tried using eval but it does not work.

      Thanks for any help.

      REPLY:
      for(i = 0; i < maxnumbersinlist; i++) {
      _root["show" + i] = myLoadVar["display" + i];
      }

        • 1. Re: Reiterative (dynamic)  Loadvars
          kglad Adobe Community Professional & MVP
          why aren't you using something obvious like:

          Question 1: send_lv.answer1 = "John";

          Question 2: send_lv.answer2 = "testing server 234";

          Question 3: send_lv.answer3 = "3"; etc.
          • 2. Reiterative (dynamic)  Loadvars
            akiva.kent Level 1
            The "obvious" hardcoding of each variable would mean that I would have to write 80-100 separate AS scripts for POSTing each question number in the test to the php file , which would not only bloat the file size and/or make very lengthy code. My current AS code is just 8 frames wide and uses only 2 frames for the questions themselves (1 frame for m/c, 1 frame for short answer). The test can be from 1 to unlimited number of questions of either type.

            Therefore, by hardcoding the variables, I limit the number of test questions. By using a dynamic loadvars, the code becomes extensible to any number of questions and uses the one POSTing script for each question.

            What confuses me in the example on the forum:

            for(i = 0; i < maxnumbersinlist; i++) {
            _root["show" + i] = myLoadVar["display" + i];
            }

            1) the example has something = myLoadVar but I have

            var send_lv:LoadVars = new LoadVars();

            How do I "define" the new LoadVars with the ["question" + i]; in it. (I've replaced "display" with "question")

            so I get a "dynamic" new LoadVars()?

            2) Then in its usage would it be
            send_lv["question"+i].nme ?

            IMPORTANTLY:

            The following give syntax errors in Flash CS3:
            var result_lv["question"+i]:LoadVars = new LoadVars(); OR

            var result_lv."question"+i:LoadVars = new LoadVars();


            Thanks again, kglad. Your help is greatly appreciated.
            • 3. Re: Reiterative (dynamic)  Loadvars
              kglad Adobe Community Professional & MVP
              i didn't say anything about hardcoding. anyway, if the user answers are stored in an array (say, answerA), you can use:

              • 4. Re: Reiterative (dynamic)  Loadvars
                akiva.kent Level 1
                (My QUESTION follows this php script)

                The previous example php POSTED to was:

                <?php
                $contents = $_POST['nme'];
                echo $contents; //DOESN'T DISPLAY CONTENTS
                //open the file in write only mode - used to see the contents of the _POST variable sent by LoadVars
                $file = fopen('loadvars.txt','w');

                //write the contents
                fwrite($file, $contents);

                //close the file
                fclose($file);

                ?>
                ---------------- QUESTION FOLLOWS: Would this then be
                $contents = $_POST['answer1']; in the php file if

                send_lv["answer_"+i] = answerA ; is in the Flash swf file? where i=1 ?

                Thanks. As ever, kglad, even though I haven't implemented the code, it is way leading me in the right direction.
                I can't thank you enough.

                • 5. Re: Reiterative (dynamic)  Loadvars
                  kglad Adobe Community Professional & MVP
                  yes. i=1,2,...,40 just as shown in the for-loop. this code would run after the last question is answered.
                  • 6. Reiterative (dynamic)  Loadvars
                    akiva.kent Level 1
                    Thank you so much!

                    I am equally as confused:

                    There are several fields for each question, viz.
                    lastname, firstname, question_number, answer, number_of_tries, etc. in which I used:
                    send_lv.last = lastname value;
                    send_lv.first = firstname value;
                    send_lv.qnumber = question_number value; etc.

                    (this way the database can be queried by student name)

                    where I have saved each field in a separate array where each element of the array represents each question number, e.g.
                    lastname[1] = lastname for question 1, lastname[2] = lastname for question 2 and similarly for each of the fields for each question:
                    firstname[1] = firstname for question 1, firstname[2] = lastname for question 2

                    the send_lv.sendAndLoad("loadvars.php", result_lv, "POST");
                    would then POST all of the fields for the question to the php file which CURRENTLY inserts all of the fields correctly into ONLY 1 record of the mySQL database thereby representing ONLY ONE question.

                    I WANT TO BE ABLE TO POST ALL OF THE FIELDS FOR EACH QUESTION which inserts each question as a separate record into the mySQL database.

                    In the nice example that you provided, however, it is POSTing an array of ONE field for ALL questions.

                    ****************** THE QUESTION TO BE SOLVED **********
                    the big Kahuna to me is

                    How am I able to POST ALL of the fields for ONE question - insert the record - and POST the next question still using the SAME send_lv (LoadVars object)????

                    --> THIS WAS MY ORIGINAL PROBLEM !!!!

                    ****************** THE QUESTION TO BE SOLVED **********
                    In the example using:
                    $contents = $_POST['answer1']; in the php file if

                    send_lv["answer_"+i] = answerA; is in the Flash swf file

                    I also end up with a finite number of POST variables in the php file which I must hard-code as :
                    $contents = $_POST['answer1'];
                    $contents = $_POST['answer2'];
                    $contents = $_POST['answer3']; until the maximum number of questions...
                    unless I am able to concentate
                    $contents = $_POST['answer'.i];
                    in a for -loop where i goes from 1 to the maximum number of questions.

                    (this last issue may not be relevant if I'm NOT using the
                    send_lv["answer_"+i] = answerA

                    because it doesn't let me include all of the aforementioned fields in the one send_lv LoadVars object.

                    ... I'm back to square one!
                    thanks again
                    • 7. Re: Reiterative (dynamic)  Loadvars
                      akiva.kent Level 1
                      This is an array of loadvars objects: (from a google of "dynamic loadvars") http://board.flashkit.com/board/showthread.php?t=426133


                      Perhaps this is my answer. However, someone please explain how this means. I think that it means that each array element is a separate loadvars object. Is it possible to use each array instead of send_lv in the sendAndLoad statement that POSTs the variables to the php file. If so, someone please show how it would be done. thanks

                      mypages = [];
                      mypages[n] = new LoadVars();
                      ....
                      mypages[n].load(....)

                      • 8. Re: Reiterative (dynamic)  Loadvars
                        kglad Adobe Community Professional & MVP
                        that's an array of loadvars instances which doesn't make sense.

                        so, you want to call your php file after each question is answered? so, you're going to make 40 calls to your php file and your php file is going to execute 40 times?

                        if so, just use one loadvars. for each question, define the loadvars' properties that you want to send to your php file, null any previous properties that you don't want to send to your php file and execute a send method.
                        • 9. Re: Reiterative (dynamic)  Loadvars
                          akiva.kent Level 1
                          Again, as I described above, I'd have to limit the number of my test questions to the number of loadvars objects, e.g. send_lv1, send_lv2, etc. AND I'd have gigantic long script for all these LoadVars objects INSTEAD of a one LoadVars object that can be reused. Any ideas around this one?
                          • 10. Re: Reiterative (dynamic)  Loadvars
                            kglad Adobe Community Professional & MVP
                            again:

                            if so, just use one loadvars. for each question, define the loadvars' properties that you want to send to your php file, null any previous properties that you don't want to send to your php file and execute a send method.
                            • 11. Reiterative (dynamic)  Loadvars
                              akiva.kent Level 1
                              your code does the trick! Once again kglad is the Man!

                              I modified it to show that you can add many variables (as arrays) representing the fields from ALL of the questions, and I removed the "_" which gives a urlencoded thing into the mix.

                              the send_lv object includes ALL of the fields for ALL of the questions as name-pair values, which is what I wanted! Happy! Happy me!

                              var send_lv:LoadVars = new LoadVars();

                              totalquestions = 40; //whatever number you want

                              for(var i:Number=1;i<=totalquestions;i++){

                              //answer, lastname, firstname are fields in the mySQL database table to insert into

                              send_lv["answer"+i] = answer ;
                              send_lv["lastname"+i] = lastname
                              ;
                              send_lv["firstname"+i] = firstname ;
                              send_lv.totalquestions = 5;
                              }

                              send_lv.sendAndLoad("loadvars.php",result_lv,"POST");

                              ----------------
                              The for loop can imclude as many fields as you need! I set up the SWF file to sendAndLoad to the php file for Totalquestions = 5 , as a prototype. I created several fields and was able to POST them to a php file that inserted 5 records.

                              The psedocode for the php file is:

                              $variable1 = username; //repeat for password, localhost, etc.

                              $variable2 = $_POST_{'totalquestions' ];

                              for ($i = 1; $i <= variable2; $i++) {
                              $variable3 = $_POST_{'field1' ].$i //field1
                              $variable4 = $_POST_{'field2' ].$i //field2 //repeat this for all your fields

                              // open the database


                              $qry_val1 = 'SELECT * FROM yourtablename (field1, field2) VALUES (" '.$variable3.' ", (" '.$variable3.' ")';
                              $result - mysql_query($qry_val2) or die( 'could not query database'. mysql_error() );

                              } //for loop ends

                              mysql_close(db$) //close the database
                              }

                              I'm taking the time to put this here to help others if they search the forum instead of asking the same thing again.
                              I have to admit that dealing with databases is REALLY hard the first time.

                              thanks again all, especially kglad!
                              • 12. Re: Reiterative (dynamic)  Loadvars
                                kglad Adobe Community Professional & MVP
                                you're welcome.