13 Replies Latest reply on Jan 11, 2007 2:01 AM by ojk007

    saving list data to text file?

    ][odine
      Hey, continuing on my project, im looking into how i would go about saving the contents of a list component to a .txt file on the server?

      the list data would have to be compiled into a format that can be read by the method im using to load it there in the first place:

      var loader:URLLoader = new URLLoader(new URLRequest("allsections.txt"));

      private function loaddata(event:Event):void {
      var variables:URLVariables = new URLVariables(loader.data);
      var sectionNameArray:Array = variables.sectionNames.split(",");
      srclist.dataProvider=sectionNameArray;

      with the loaded text file looking like:

      sectionNames=January,February,March,April,May,June,July,August,September,October,November, December

      so the text file is loaded into an array then split to be shown in the list as each its own item.

      I would then have a button, which takes all the items in a list, and save them back into the format of the text file above.

      i.e "variable=item1, item2, item3..."

      thoughts anyone?

      --][--
        • 1. Re: saving list data to text file?
          M. Huisman Level 1
          In the current Flex release it's not possible to write to the file system.

          You should wait for apollo to do this.

          (or use 3rd party software like zinc).
          • 2. Re: saving list data to text file?
            ][odine Level 1
            surely there must be a way to write a simple text file...flash can do it...why cant Flex? I imagine php or something must be used somehow to accomplish this...
            • 3. Re: saving list data to text file?
              M. Huisman Level 1
              Yea, i think it's possible to write txt using php, but weird as it is, flex can't write anything accept the sharedobjects.
              • 4. Re: saving list data to text file?
                ][odine Level 1
                well...the only reason i need to write to a text file (on the server by the way, not users system) is because my list components are using text files as dataProviders. if you look at what im working with ( http://mattgroth.echointernet.net/test/main.html admin/pass for login, and click administrate on botom left) you will see i have 2 lists, which you can drag to and from each other. I want to be able to click save, and save the state of each list. And when the app is run again, or by another user, they see the saved list.

                So thats why im using text files. if anyone can think of a better solution i would be happy to hear it. if it would be easier than somehow figureing out how to get flex to write to a text file, im all for it!

                thanks guys in advance!

                --][--
                • 5. Re: saving list data to text file?
                  ojk007
                  this is crude an unfinished, but if your familiar with php it shouldnt be too hard.

                  Basically you pass all the info through _POST vars to the filename.php

                  then dump them into the file

                  <?php
                  $var1 = $_POST['var1'];
                  $entry_line = "<log var1=$var1>";
                  $fp = fopen("yourfile.txt", "a"); // "a" will append, meaing it will add on, "w" will rewrite the entire file
                  fputs($fp, $entry_line);
                  fclose($fp);
                  ?>
                  • 6. Re: saving list data to text file?
                    Hugo_Core Level 1
                    exact PHP is the solution.
                    • 7. Re: saving list data to text file?
                      ][odine Level 1
                      hugo would you care to elaborate a little more? not quite sure what you mean.

                      @ojk - i am trying to figure out the php there, it seems to be in order? what needs to be completed on it? As well...how wold i go about pulling the data from the list, putting it in one continuous string with "," (i.e item1, item2, item3) and then write that to the text file? I am obviously having a hard time getting my head around it.
                      • 8. Re: saving list data to text file?
                        ntsiii Level 3
                        You are asking for a zillion things at once. You need to break this up into parts.

                        First set up the PHP to save and retrieve files based on querystring parameters. Hard code the logic so you can test the php page. Use XML as the format for your data because you can view it in a browser.

                        When the php part is working, create a simple Flex app tht uses HTTPService to load and save simple, hard coded (xml) text .

                        When you can do that, decide how you will package the data (use xml). Get your real app to build the xml and display it in an alert box.

                        When you can do that set up the real app with the HTTPService code from the test app, pass the xml to php.

                        If you have done all the steps correctly and completely, it will work the first time.

                        Tracy
                        • 9. saving list data to text file?
                          ][odine Level 1
                          Tracy,

                          I know i ask a zillion things at once, but im just so excited about flex2 i just wanna know it all! lol. so please pardon my randomness.

                          Your approach deffinatly seems more logical, and i was thinking xml beforehand, i just have no idea how to work with it in as far as php/flex2 go. You make it sound so simple, first do this, then this, then that and boom, finished! but my issue is this:

                          i have no idea how to do these things, thats why im asking for help :S setup PHP to save and retrieve files based on querystring parameters. hard code the logic...i mean...that means nothing to me. It makes sense - if you get what i mean, but i have no idea how to accomplish it.

                          I guess before asking what the heck step two ment, i should stop here and work at step one right! lol..see, Im learning...at least trying. Ill start scouring the help files i guess.

                          --][--
                          • 10. Re: saving list data to text file?
                            ][odine Level 1
                            I have been looking at it (tracy, i think you did an example somewhere i found?) anyways, it was WAY more than i need...and im starting to get the feeling that making the whole xml system work is not only overkill for what im doing, but also much more complicated, i mean what, with all the nodes and parsing? vs 1 line of variable? i dunno...its not like i know how to do that either though right? and i guess i better learn the most efficient way, and the way most would do it. Is there anything you guys can offer as far as the XML side of this goes? how i would go about it? tracy was a great help...but unfortunatly nothing of what she said makes any sense to me :S

                            --][--
                            • 11. saving list data to text file?
                              ojk007 Level 1
                              Alright lets simplify things, you want to write data from flex into a ".txt" file. Due to security reasons, flex cannot directly edit a file, INTSTEAD you need to tell another file to do it. IT works EXACTLY like ajax, just looks alot better!

                              So firstly work out what you want your text file to look like.

                              Then, work out how you are going to send it out from flex, _POST vars are by far the easiest.

                              Then create your php file.

                              Honestly XML seems the easiest way IMHO.

                              Heres how to send data through _POST vars to a php file
                              ----------
                              <mx:HTTPService id="myHTTPService" url="yourFile.php" useProxy="false">
                              <mx:request xmlns="">
                              <var1>first var</var1>
                              <var2>second var</var2>
                              </mx:request>
                              </mx:HTTPService>
                              ----------

                              then the php
                              ---------
                              <?php
                              $var1 = $_POST['var1'];
                              $var2 = $_POST['var2'];
                              $entry_line = "<var1>$var1</var1>
                              < var2>$var2</var2>";
                              $fp = fopen("yourfile.txt", "a"); // "a" will append, meaing it will add on, "w" will rewrite the entire file
                              fputs($fp, $entry_line);
                              fclose($fp);
                              ?>
                              ---------

                              So your text file will look like:
                              ----------
                              <var1>first var</var1>
                              < var2>second var</var2>
                              ----------


                              The question is, do you know how many variables you need to write to you ".txt" file?



                              EDIT: xml is NEVER overkill, simply because it is SOOOO easy to work with.
                              After all flex was Built for it!


                              EDIT2: after re-reading you first post if it is possible to reconstruct the original string "1,2,3,4" you could just pass one varible of "1,2,3,etc.." to the php file instead of one for each variable, jsut a thought but it may work.
                              • 12. saving list data to text file?
                                ][odine Level 1
                                Hey, now thats starting to make a little more sense! thanks!

                                First to answer your questions,
                                I dont know how many variables there will be - depends solely on how many the user decides to drag over and hit save.

                                If the list as you described it can be assigned to a dataholder, i dont see why needing to stay with txt files.

                                how would i add that to init when the lists were loaded (when the state loads)? remember i need the list components to be populated by these same xml files were writing. Basically, there are 2 lists, one blank, or full of choiced, you drag and drop from the choices to the blank one, then hit save, and it saves BOTH lists, so that when you go back to view the page there will be no blank lists as its saved the items you have dragged over.

                                The lists should be populated when the state loads in, and the save should happen on a buttn press. I will start having a play with the stuff, getting the xml and php sorted. I am slightly concerned about how to get an xml file like
                                ----------
                                <var1>first var</var1>
                                < var2>second var</var2>
                                ----------
                                to be assigned as the dataProvider for the list?

                                Cheers Ojk, fantastic help!

                                --][--
                                • 13. Re: saving list data to text file?
                                  ojk007 Level 1
                                  where are they being saved?

                                  Now it seems xml MAY not be the smartest choice here, contrary to what i said previously, this is because you have an unknown number of vars.

                                  you could try this, using just one var. Assemble the var in AS so it looks like "1,2,3,4,5" and pass that var to the php file which will write it.
                                  ----------
                                  <?php
                                  $var1 = $_POST['var1'];
                                  $fp = fopen("yourfile.txt", "w");
                                  fputs($fp, $var1);
                                  fclose($fp);
                                  ?>
                                  ----------