how to parse this,and input in mysql ?!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blitzztriger
    New Member
    • Jul 2007
    • 10

    how to parse this,and input in mysql ?!

    Hello all!
    This might be simple,but im having some problems in parsing the php code, can someone help?
    i have a text file with this (for example):
    --------------------------------------------------------------------
    blitzztriger [****] - time : 1


    hello - ( 47) 52 40
    30,000 (+600) 50% 20,000 (+400) 50%

    hello2 - ( 48) 102 40
    35,000 (+650) 100% 50,000 (+200) 90%
    --------------------------------------------------------------------
    the ideia is to :
    "name" --->column 1(line1)the dump will be:blitzztriger
    "hello"--->column 2(line1) the dump will be:hello (hello its a name)
    "( 47)"--->column 3 (line1)the dump will be:47
    "( 52)"--->column 4 (line1)the dump will be:52
    "30,000"--->comumn 5(line1)the dump will be: 30000 (number)
    "(+600)"--->column 6(line1)the dump will be : 600
    "50%"------>colum 7(line1) the dump will be: 50
    and so on...

    "name" --->column 1(line2)the dump will be:blitzztriger
    "hello2"-->column 2(line2) the dump will be:hello2 (hello2 its a name)
    "( 48)"--->column 3 (line2)the dump will be:48
    "(102)"--->column 4 (line2)the dump will be:102
    "35,000"--->comumn 5(line2)the dump will be: 35000 (number)
    "(+650)"--->column 6(line2)the dump will be : 650
    "100%"----->colum 7(line2) the dump will be: 100
    and so on...

    then the dumped values should enter mysql DB.
    i already tried millions of things but no way!! going mad... :(
    thanks in advance
  • blitzztriger
    New Member
    • Jul 2007
    • 10

    #2
    hum, if its difficult, can someone show me just one example?!

    Comment

    • adamalton
      New Member
      • Feb 2007
      • 93

      #3
      I don't quite understand what it is that you're doing, but i can give a basic example of how to put stuff into a mysql database.

      Once you've created the database with all of the columns that you want (ask if you want to know how to do that, there are various ways).

      get your php script to connect to your database:
      [PHP]$link=mysql_con nect("localhost " , "username" , "password") ;
      mysql_select_db ("name_of_your_ database" , $link);[/PHP]

      Create a query to insert stuff into your database:
      [PHP]$query="INSERT INTO name_of_your_ta ble (name_of_column 1 , name_of_column2 , etc) VALUES ('value_to_go_i n_col_1' , 'value_4_col2' , 'etc')";[/PHP]
      Then execute the query:
      [PHP]mysql_query($qu ery);
      if(mysql_error( ))
      {
      print "Oh, shit!<br>";
      print mysql_error();
      }
      else
      print "The stuff was put into the database.<br> Woo!!";[/PHP]

      So you'd need to do that for each of the lines (ie records) that you want to add to the db.

      I'm sure there's a different quicker method for adding lots of records at once, but this'll get you started.

      Comment

      • blitzztriger
        New Member
        • Jul 2007
        • 10

        #4
        Thanks a lot , that part i already understood. :)
        Now for a better comprehension of what i want to do , heres the table :

        name id pieces (A) (pieces (b) working pieces (A) production (A)
        blitzztriger 47 52 40 30000 600
        blitzztriger 48 102 40 35000 650

        and so on...

        From a .txt file like this:
        blitzztriger [****] - time : 1


        hello - ( 47) 52 40
        30,000 (+600) 50% 20,000 (+400) 50%

        hello2 - ( 48) 102 40
        35,000 (+650) 100% 50,000 (+200) 90%

        I know that i must regex that file..and thats the problem...or should i put it in array´s and then connect it to the db?
        even with the correct regex code, where should i put it to send to the db btw?, in here?
        $query="INSERT INTO name_of_your_ta ble (name_of_column 1 , name_of_column2 , etc) VALUES ('value_to_go_i n_col_1' , 'value_4_col2' , 'etc')";
        value_to_go_in_ col_1 = regex code ?

        Comment

        • adamalton
          New Member
          • Feb 2007
          • 93

          #5
          I still don't understand!! Are you trying to split your text file into all of the separate items using regular expressions, and then put all of those items into the db?

          Comment

          • blitzztriger
            New Member
            • Jul 2007
            • 10

            #6
            yes!! thats it!! :D
            heres some regex for this part:
            30,000 (+600) 50% 20,000 (+400) 50% 1,000 (+60) 50% 0 (+30) 50% 20,000

            regex: ([\d,]+) \(\+([\d,]+)\) (\d+)% ([\d,]+) \(\+([\d,]+)\) (\d+)% ([\d,]+) \(\+([\d,]+)\) (\d+)% ([\d,]+) \(\+([\d,]+)\) (\d+)%

            Comment

            • adamalton
              New Member
              • Feb 2007
              • 93

              #7
              I'm not hot on regular expressions, but my approach would be something like this:

              Use fopen() or something to open the text file and stick the contents of it into a variable, $text.

              Then something like this:
              [PHP]while(preg_matc h("/hello/",$text))//while there is still at least one more record to do
              {
              list($junk,$tex t) = preg_split("/hello/",$text); //cut off stuff up to (and including) "hello"
              list($junk,$tex t) = preg_split("/(/",$text); //cut stuff off up to the bracket (
              list($value1,$t ext) = preg_split("/)/",$text); //so $value1 now contains the number that was between the brackets ()
              list($junk,$tex t) = preg_split("/ /",$text);//cut off the space that comes after the closing bracket
              list($value2,$t ext) = preg_split("/ /",$text); //$value2 now contains the number that comes before the next space (52 in the first one)
              //just keep going through the bits until you've got all of the values into $variables.
              //then add the dogs to the database:
              $query="INSERT INTO table (column1,column 2,column3,etc) VALUES ('". $value1 ."','". $value2 ."','". $value3 ."','". $etc ."')";
              mysql_query($qu ery);
              }[/PHP]
              I hope that helps

              Comment

              Working...