12 Replies Latest reply on Aug 18, 2010 5:36 AM by cyrus@adobe

    Adobe flex parsing a text file and save to database.

    cyrus@adobe

      hi to all.. does anyone know how to parse a text file to adobe flex and save it to database(mysql)..

      to illustrate, i have a text file containing:

       

      student.txt

      format is:

      [firstname] [lastname] [age]

      ex.

       

      Kimber Stratum 20

      Alexis Abemojar 21

      ......  ....... ......

       

      what i want to do is to read that text file and save it to database.


      my database  contains..

      table: student      Field: fName, lName, age

       

      by the way i'm using MySql for database and adobe flex 3.

       

      tnx..hope you can help me.

        • 1. Re: Adobe flex parsing a text file and save to database.
          Matt Le Fevre Level 4

          you'll need some form of script (PHP etc...) to access the database.

           

          to parse the string, i'd also use a script (i don't see any actual need for Flex here, but i imagine you havn't stated everything you want this project to do)

           

           

           

          your text file formatting isn't the most ideal, but if it's exactly as you say:

           

          [First Name] [Last Name] [Age]

          [First Name] [Last Name] [Age]

          [First Name] [Last Name] [Age]

           

          alll the way through, it shouldn't be too hard, you can do the entire thing using a small PHP script in 4 stages.

           

           

           

          1) Open the file

           

          2) use the explode() function on "\n", this will leave you with an array and each index in the array will contain a single person, you can loop through this. As you do, perform step 3

           

          3) you'll now need to split up each of the information contained in each index, you could use the PHP explode() function again on " " (a space) which in theory would work exactly the way you want, as you'll now have the first name/last name/age split up into 3 parts and ready to be inserted into the database.

           

          But i imagine you'll have several people in your text file with annoying names, such as "Le Fevre" which'll mess things up a little, but not much. Just perform a loop on your information, the first index of the array will always be the first name, the last index will always be the age, anything inbetween will be the last name.

           

          4) after each seperation of the information in step 3, simply input the information into your database using a query.

           

          eg:

           

          <?php
          
              mysql_connect("localhost","root","");
              mysql_select_db("test_db");
              
              $myFile = "test.txt";
              $fh = fopen($myFile, 'r');
              $theData = fread($fh, filesize($myFile));
              fclose($fh);
              
              $person = explode("\n", $theData);
              $count = 0;
              
              while($person[$count]){
                  $nameParts = explode(" ", $person[$count]);
                  
                  $firstName = array_shift($nameParts);
                  $age = array_pop($nameParts);
                  
                  if(count($nameParts) > 1){
                      $count2 = 0;
                      while($nameParts[$count2]){
                          $secondName .= $nameParts[$count2]." ";
                          $count2++;
                      }
                      $secondName = substr_replace($secondName,"",-1);
                  }else{
                      $secondName = $nameParts[0];
                  }
                  
                  mysql_query("INSERT INTO student (fName, lName, age)VALUES ('$firstName', '$secondName', '$age')");
                  
                  $count++;
              }
              
          ?>
          
          • 2. Re: Adobe flex parsing a text file and save to database.
            cyrus@adobe Level 1

            thank you sir...this would be so helpful... : )


            ahmm.. but you think sir it is necessary to use adobe flex here or just a simple PHP script?

            • 3. Re: Adobe flex parsing a text file and save to database.
              Matt Le Fevre Level 4

              well from what you've said, i don't see the need for flex at all.

               

              Flex is good at displaying information, which isn't necessary here as you're simply adding stuff to a database from a file.

               

               

               

              If you then wanted to go on to make an application that can add people to a database through an input form, then i'd see a need for flex.

              • 4. Re: Adobe flex parsing a text file and save to database.
                cyrus@adobe Level 1

                oh..i see..tnx a lot sir..i tried the code you provided and it works... : )

                 

                this will be very helpful..thank you very much..

                • 5. Re: Adobe flex parsing a text file and save to database.
                  cyrus@adobe Level 1

                  ahmm. sir i have a concern. . what if the firstname of the student is something like this.. Mark Anthony? or John Michael Burias? ..

                  how can i parse that into one?

                   

                  tnx sir.

                  • 6. Re: Adobe flex parsing a text file and save to database.
                    Matt Le Fevre Level 4

                    then you're going to need a better formatted source file.

                     

                    currently, the only thing seperating different objects is a new line "\n", which is good enough, it's all you really need.

                     

                     

                    But the first/last names within each object cannot be intelligently distinguished because there is nothing solidly seperating them/identifying where they are. Only spaces, which not only seperate the firstname/lastname/age, but also sub-seperate the last name on occasions into different pieces, and now also do the same for the first name.

                     

                    so if the names really are that diverse, you're going to need a seperator between them. something like

                     

                    first name(s)#last name(s)#age

                     

                    eg:

                     

                    Mark Anthony#Peters#41

                    John Michael Burias#Johnson#23

                    Matt#Le Fevre#96

                     

                    etc..

                     

                    or, if possible join first / last names with hypens temporarily

                     

                    eg:

                     

                    Mark-Anthony Peters 41

                    John-Michael-Burias Johnson 23

                     

                    etc...

                     

                    until then, it's impossible to get it 100% right, as your conditions are too complicated for something that has no rules.

                    • 7. Re: Adobe flex parsing a text file and save to database.
                      cyrus@adobe Level 1

                      ahh.. i see..now i know sir...

                      ahmm..

                      thank you for your reply sir although it's quite long..hehe : )

                       

                      you helped me a lot sir...

                      thanks again...

                      • 8. Re: Adobe flex parsing a text file and save to database.
                        ny2laweb

                        Hello,

                         

                        Wonder if I can join this thread since I'm dealing with a similar issue that I can't figure out.

                         

                        I have a website that allows users to upload files to the server.  Through flex, I have functionality to rename (edit) the filename of what's being uploaded BEFORE uploading. This can be done for individual files or files uploaded in a batch (which is why flex is needed).

                         

                        When upload completes a php script passes the information to the database which has a table with a title field and a url field.

                         

                        When upload is complete, for some reason, the original filename never changes and gets passed to the server, and  the updated (edited) filename is passed via  PHP script to the db to BOTH the title and url fields.

                         

                        This is a problem because the database is now out of sync with the server.  The ideal solution would be for the edited filename to change ONLY the title field in the database.  The other solution (to change the unedited filename on the server which flex was suppose to do) seems too cumbersome.

                         

                        There does not appear to be a problem in the php script so I can only assume it's in the flex.  I'm new at using flex. I'm hoping you can give me some pointers on how best to fix this.

                         

                        Message was edited by: ny2laweb

                        • 9. Re: Adobe flex parsing a text file and save to database.
                          cyrus@adobe Level 1

                          hello again sir.. i have another problem of parsing.. what if sir i have for fields or column?

                          example, fname lname time date?

                           

                          i tried the code you provided.. however, it only parses fname and lname but the time and date does not..

                          what should i do regarding to this problem sir?

                          • 10. Re: Adobe flex parsing a text file and save to database.
                            Matt Le Fevre Level 4

                            if your just adding to additional fields, you just need to alter the code i've given you.

                             

                            It's not that difficult, so i'd suggest reading through it until you understand it. Just a few basic principles you need to understand before you go any further.

                             

                            read up on the logic behind it, if you can't work it out then come back and show me what you've tried and i'll help you through it

                            • 11. Re: Adobe flex parsing a text file and save to database.
                              flexme7

                              ahh..ok sir..

                              i understand a little about the code...

                              but i made this php base from yours..

                               

                               

                              <?php

                                  mysql_connect("localhost","root","");
                                  mysql_select_db("dbName");
                                 
                                  $myFile = "readme.txt";
                                  $fh = fopen($myFile, 'r');
                                  $theData = fread($fh, filesize($myFile));
                                  fclose($fh);
                                 
                                  $person = explode("\n", $theData);
                                  $count = 0;
                                 
                                  while($person[$count]){
                                      $nameParts = explode(" ", $person[$count]);
                                     
                                      $firstname = array_shift($nameParts);
                                      $time = array_pop($nameParts);
                                      $date = array_unshift($nameParts);
                                     
                                      if(count($nameParts) > 1){
                                          $count2 = 0;
                                          while($nameParts[$count2]){
                                              $secondname .= $nameParts[$count2]." ";
                                              $count2++;
                                          }
                                          $secondname= substr_replace($btname,"",-1);
                                      }else{
                                          $secondname= $nameParts[0];
                                      }
                                       //$date = array_pop($nameParts);
                                     
                                      mysql_query("INSERT INTO records (firstname, secondname, logtime, date)VALUES ('$firstname', '$secondname', '$time','$date')");
                                     
                                      $count++;
                                  }
                                 
                              ?>

                               

                               

                              -----

                               

                              i'm not sure with array_unshift..i only tried that..^^

                              but i know array_pop is, it returns and assign the last value to a variable..

                              however, still confused sir..

                               

                              thanks sir. 

                              • 12. Re: Adobe flex parsing a text file and save to database.
                                cyrus@adobe Level 1

                                oppss.. my mistake sir i did'nt notice i've used my other account which has screen named as "flexme7"..

                                 

                                however sir, i just copy and paste it to post again.. here sir..

                                 

                                 

                                -------

                                ahh..ok sir..

                                i understand a little about the code...

                                but i made this php base from yours..

                                 

                                 

                                <?php

                                    mysql_connect("localhost","root","");
                                    mysql_select_db("dbName");
                                   
                                    $myFile = "readme.txt";
                                    $fh = fopen($myFile, 'r');
                                    $theData = fread($fh, filesize($myFile));
                                    fclose($fh);
                                   
                                    $person = explode("\n", $theData);
                                    $count = 0;
                                   
                                    while($person[$count]){
                                        $nameParts = explode(" ", $person[$count]);
                                       
                                        $firstname = array_shift($nameParts);
                                        $time = array_pop($nameParts);
                                        $date = array_unshift($nameParts);
                                       
                                        if(count($nameParts) > 1){
                                            $count2 = 0;
                                            while($nameParts[$count2]){
                                                $secondname .= $nameParts[$count2]." ";
                                                $count2++;
                                            }
                                            $secondname= substr_replace($btname,"",-1);
                                        }else{
                                            $secondname= $nameParts[0];
                                        }
                                         //$date = array_pop($nameParts);
                                       
                                        mysql_query("INSERT INTO records (firstname, secondname, logtime, date)VALUES ('$firstname', '$secondname', '$time','$date')");
                                       
                                        $count++;
                                    }
                                   
                                ?>

                                 

                                 

                                -----

                                 

                                i'm not sure with array_unshift..i only tried that..^^

                                but i know array_pop is, it returns and assign the last value to a variable..

                                however, still confused sir..

                                 

                                thanks sir. 

                                 

                                ----