26 Replies Latest reply on Jun 30, 2007 12:11 AM by Greg Dove

    Importing portions of a .txt file

    Sketchsta Level 1
      I havent imported text from external .txt files before, but im sure it cant be too hard.
      However, im wondering if its possible to import Sections of one text file, into separated dynamic text fields.

      Basically this is for a testimonials page, that will be updated constantly. and im looking to make this as easy as possible for the client to update by themselves.

      What im thinking at the moment is to have one external text file, which will contain all testimonials ( current and future ), and for flash to import each testimonial separately into different text fields, which are created and separated dynamically.

      Is this possible?
        • 1. Re: Importing portions of a .txt file
          Greg Dove Level 4
          Yes its possible.
          You could do it with a plain text file and have some sequence which is used to split or identify the start and end of particular sections.
          LoadVars.onData ( http://livedocs.adobe.com/flash/8/main/00002332.html#wp290327 )
          lets you intercept the standard var1=value1&var2=value2 parsing that flash does and so removes this requirement for how the text file is interpreted by flash. This means you essentially have one big string loaded from the file that you can play with. You can use String.split("!Section!") for example if you had the sequence !Section! to delineate your testimonials.

          Or you could do it with an xml file where the start and end of each section (and other parts of structured data) are represented by xml tags.
          • 2. Re: Importing portions of a .txt file
            Sketchsta Level 1
            I did first think about using a XML file, but to people who dont know what to do, it can get quite daunting. So im trying to make this Testimonials page with just a text file, where the client can simply copy and paste new testimonials.

            Like i said, i havent done this before, which means, i preety much have no idea what you just said.. =P

            what do i use to read the sequence for start and end of a text portion?
            how would i create a text filed for each start/end sequence in the text file?
            • 3. Importing portions of a .txt file
              Greg Dove Level 4
              Sorry... are you familiar with using LoadVars?

              In your text file you will need to have some way to 'mark' the separate testimonials (kind of like how excel can import comma separated values or csv files, where the numbers are separated by commas). So you need to decide what that is. E.g. if you wanted to use [TESTIMONIAL] as a split between the different testimonials... then it might look like this inside your text file:

              "Person X was the best person I ever worked with.
              They delivered on-time and on-budget."
              -Somebody important

              [TESTIMONIAL]
              "Person X was the best person I ever worked with.
              They delivered early and under budget."
              -Somebody else important

              [TESTIMONIAL]
              "Person X was OK to work with.
              They didn't overcharge me or annoy me too much."
              -Another important person


              Here the first testimonial doesn't have [TESTIMONIAL] before it. It probably makes sense to make it so it does I guess so its more intuitive for your client . It just needs an extra line of code to handle it.
              If its set up like this, then in the LoadVars onData handler, all that's requred to put the testimonials into separate array elements is to use something like
              myTestimonials = src.split("[TESTIMONIAL]")
              inside the onData handler. (using the example in the livedocs link I gave in the earlier post)

              Let me know if you need an example with a LoadVars.
              • 4. Re: Importing portions of a .txt file
                Greg Dove Level 4
                The second part of your request could be done a number of different ways.

                Creating textfields for each testimonial is one option. But you need to consider placement if there are too many testimonials for the space...e.g. inside a scrollPane perhaps

                Another option is to browse the testimonials in the same textfield or textArea, using buttons to change the index that you display from the array. This is probably easier...but perhaps its not the impression your client wants (I'm guessing they prefer having lots of glowing testimonials on-screen at the same time).

                Let me know if this is not clear.
                • 5. Re: Importing portions of a .txt file
                  Sketchsta Level 1
                  thanx for the help GWD,
                  I'm not too familiar with LoadVars(), only time i use that is for Contact Forms.

                  as for the placements of each text field, couldnt i specify something like auto-resize, multiline...then for the next text field, to place it 20px below the previous?

                  I know exactly what you mean, it would be simpler to just have one text field to dysplay each testimonial, im not sure if the boss would mind that, he didnt specificly say " I want alot of testimonials showing." so i guess i could do that (and hope he doesnt complain) but also, each of the testimonials i have now, they are only like 1 to 2 lines of text.
                  If i were to show just one at a time, the page would have too much free space.

                  Im gonna try your previous post, and see how i go.
                  how tho, can i tell flash to create a new textField for each split?
                  • 6. Importing portions of a .txt file
                    Greg Dove Level 4
                    Ok... so after you've processed the array from LoadVars.onData... (let me know if you strike difficulty there after trying and I'll take a shot at it.)

                    Here's a simple example of putting the array of text on screen into separate textfields.
                    EDIT : fixed typo in code .wordwrap should be .wordWrap

                    • 7. Re: Importing portions of a .txt file
                      Greg Dove Level 4
                      Just a quick note on that last code. If you're using a clip you made in the IDE for myTestimonialHolder (recommended) then you just need to make sure you have it's registration point at 0,0.
                      • 8. Re: Importing portions of a .txt file
                        Sketchsta Level 1
                        thats perfect...except, how can that load from a text file instead of the array?


                        I am having problems with it ( as you probably already knew ) I need to gt some sleep before trying to learn something new =P

                        I added src.split() to the code from the liveDocs, but now im not getting anything showing in the text field..
                        what am i missing?


                        my_lv.onData = function(src:String) {
                        content_ta = src.split("[testi]");
                        if (src == undefined) {
                        trace("Error loading content.");
                        return;
                        }
                        content_ta.text = src;
                        };
                        my_lv.load("testimonials.txt", my_lv, "GET");
                        • 9. Re: Importing portions of a .txt file
                          Greg Dove Level 4
                          Try this:

                          var my_lv:LoadVars = new LoadVars();
                          var myTestimonials:Array = new Array()
                          my_lv.onData = function(src:String) {

                          if (src == undefined) {
                          trace("Error loading content.");
                          return;
                          }
                          myTestimonials = src.split("[testi]");
                          trace("testimonials are: "+myTestimonials)
                          //call the funcion to display the myTestimonials here.

                          };
                          my_lv.load("testimonials.txt");
                          • 10. Re: Importing portions of a .txt file
                            Sketchsta Level 1
                            Thanx alot dude, thats exactly what i wanted. =)
                            this should be easy enough for anyone to update.

                            One last question, im going to be scrolling the movieClip containing the textFields depending on how many Testimonials there are.
                            My question is, how can i resize ( _yscale ) of the holder movieClip to fit all the Testimonials?
                            • 11. Re: Importing portions of a .txt file
                              Greg Dove Level 4
                              I tested it also... here's the full code that works for me. (I used [TESTIMONIALS] for my split marker in the text file)
                              note I had to do something to correct the carriage-return/line-feed combination that notepad gave me in Windows.

                              I'll answer your latest question in the next post.
                              • 12. Importing portions of a .txt file
                                Greg Dove Level 4
                                OK... to answer your last question... here's how I would do it:

                                Inside the myTestimonialHolder movieclip (its easy in the IDE) I would setup another movieclip inside which is called background.

                                Then, as each texfield is added with the loop, calculate its _y + its _height .
                                If its greater than background's _height property, then increase backgrounds _height to that value.

                                (or something along those lines)
                                • 13. Re: Importing portions of a .txt file
                                  Sketchsta Level 1
                                  it looks the same to me, how come you had to do that?
                                  do you think this will have some effect on different computers once its uploaded? Like some computers might not dysplay the testimonials right?

                                  heres the code i was using, and got the same output. ( minus the box drawn inside holder )
                                  • 14. Importing portions of a .txt file
                                    Greg Dove Level 4
                                    ? you mean the :

                                    //Using notepad Save-AS UTF-8 encoding
                                    //I needed to do this: (converts CRLF to CR)
                                    src = src.split(unescape("%0D%0A")).join(unescape("%0D"));

                                    part?

                                    Well I've encountered it before and used that to solve it. It should (I believe) do no harm. What was happening to me was that I was getting two newlines for every one in my source file. It's more to do with the format of the source file than what should appear on others computers once they're loaded into flash. As far as I know the newline in flash is just character 13 or CR (carriage return). Windows uses CR+LF for a newline marker. Perhaps its just me - maybe I'm not setting something up right when I save it with notepad. Or perhaps I should be using a different text editor.

                                    • 15. Re: Importing portions of a .txt file
                                      Sketchsta Level 1
                                      ohh wait a minute... i see the difference now. or atleast one difference..

                                      there are less line breaks in the output using your code.. Is that a part you thought to fix up?
                                      • 16. Re: Importing portions of a .txt file
                                        Greg Dove Level 4
                                        Yes that's it. That line fixes the extra line breaks.
                                        • 17. Re: Importing portions of a .txt file
                                          Sketchsta Level 1
                                          yeah, i know what you mean now. I only just saw that. =)
                                          • 18. Re: Importing portions of a .txt file
                                            Sketchsta Level 1
                                            hahaha... IRC would have been usefull in the past 5mins.. instead of posts. =)
                                            • 19. Re: Importing portions of a .txt file
                                              Sketchsta Level 1
                                              ok, i have this for the resizing part. seems to be ok. But does this ( background ) resize myTestimonialHolder mc too?

                                              for the scrolling, im using if() statements to check the _y position, and stop once it reaches a certain _y value. Im thinking, instead of that, i could specify the _yscale as the limit point, that way it will always change when the testimonials text file is updated.

                                              right?
                                              Am i forgetting something?
                                              • 20. Re: Importing portions of a .txt file
                                                Greg Dove Level 4
                                                quote:


                                                hahaha... IRC would have been usefull in the past 5mins.. instead of posts. =)



                                                lol... I just checked in after leaving for a meal - time to put the kids to bed... will catch up in an hour or so and check your latest post. I wouldn't use _yscale there... but let me try it when I get back.
                                                • 21. Re: Importing portions of a .txt file
                                                  Greg Dove Level 4
                                                  OK...back a little earlier than I expected.

                                                  Here's how I'd do it. It was easier than I thought because we already had the nexty variable set up.
                                                  • 22. Re: Importing portions of a .txt file
                                                    Sketchsta Level 1
                                                    ohh damn... i didnt see your posts cuz they went to the next page... oops!!

                                                    anyway, thanx for the code, i will have a look at it when i get back later today...im goin to see TRANSFORMERS!!!! yeahhhhhhh!!!!!
                                                    been waiting for months!!! =P now it's finally here!!!
                                                    • 23. Importing portions of a .txt file
                                                      Sketchsta Level 1
                                                      Hey GWD, i've gone ahead and used the code you made, and altered my scroller for it. It works good. Thanx alot dude, this is exactly what i wanted.

                                                      just curious... see the src.split() code, to split the text...is there another similar command that will allow me to apply a separate textFormat() to selected text portions?
                                                      I'm thinking of adding something like a CSS chart, with two font styles, one for the testimonials, and the other for the names of the customers.
                                                      • 24. Re: Importing portions of a .txt file
                                                        Greg Dove Level 4
                                                        After you split it once, you have the array of testimonial 'records'

                                                        There's nothing to stop you putting a second levels of structure inside those... the only thing is you're getting not too far away from how you would do it in XML... I guess its still easier for someone who doesn't like xml though.

                                                        So when it comes time to assign the myTestimonials array items to each new textfield...all you have to do is set up a function split the "testimonial" strings by whatever markers you've set up inside them. You could use the String.split method which each time will give you an array of 2 elements each time you do it... that means for subsequent splits you work off the second element from the previous split (there's assumptions there about the order and presence of the markers in each 'record'). Or just pick them out of the string with indexOf("[CUSTOMERNAME]") and a little bit of math might be easier if you've got a few of them. Once you've split them - however you do it - you have the ability to check the lengths of the different text blocks for each different textformat you want to assign to them after you concatenate them back together and assign the value to the textfield.

                                                        [TESTIMONIAL]
                                                        This is testimonial number 1.
                                                        [CUSTOMERNAME]
                                                        Mr Creosote
                                                        [CONTENT]
                                                        I highly recommend the new Transformers movie.

                                                        [TESTIMONIAL]
                                                        etc.

                                                        Hope that makes sense. I can't stick around and do it tonight, sorry. Got a family activity to go to.
                                                        • 25. Re: Importing portions of a .txt file
                                                          Sketchsta Level 1
                                                          no don't be sorry, you've been alot of help, im verry grateful.
                                                          Spend all the time you can with the family, nothing more important than that.

                                                          as for using XML I would have preferred to use XML for this, but the client wants to be able to update the page with new textimonials as they come.
                                                          I figured it would be alot less daunting for him to deal with a simple Notepad, instead of XML.

                                                          Plus, this is good learning, I didnt know loading from text file was this versatile.
                                                          I think I understand what to do. i'll have a play around with it, and let you know how i go.
                                                          I think i'll go for the indexOf() first, i remember reading about that abit, maybe i'll get lucky. =)
                                                          thanx again for all your help.

                                                          P.S... Transformers was soOOooOOOooo worth it... it's a must see movie. Even if you aren't a fan, it's just a reallly well made movie.
                                                          Might not be suitable for kids tho....not the really young ones anyway.
                                                          • 26. Re: Importing portions of a .txt file
                                                            Greg Dove Level 4
                                                            You're welcome. Good luck with it.

                                                            quote:

                                                            I didnt know loading from text file was this versatile.


                                                            I guess its not day-to-day stuff doing things this way. I think I discovered it when I had a similar requirement to 'read' a text file and saw it was possible via 'custom parsing' through LoadVars.onData . I just did a quick search now and there are some tutorials on it. e.g. http://www.bit-101.com/blog/?p=554

                                                            I've also used this approach with xml files using the XML.onData handler (e.g. the CRLF replacement with CR for windows documents) and sometimes to change things quickly in the xml (with loops) - before treating the xml like normal - in order to test new xml formats without having to edit the original xml.

                                                            Thanks for the movie recommendation - I'll keep it in mind.