Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Aug 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Separating parts of data into different rows

    I wasn't quite sure where to post this so I'm sorry if it's in the wrong category. I have a great deal of data I need to enter into my database which starts in the form xxxxxx:xxxxxx where the x's always vary in length and there is always a ":" dividing the two parts of data. I need to seperate these two parts of data and have the first part before the ":" go into one row and the second half after the ":" go into another row (both rows in same table)...the ":" can be discarded. I'll be adding this data manually by uploading a txt file to the database in the form of:

    xxxx:xxxx
    xxxxxxxx:xx
    xxxxxxx:xxxxxxx
    xxx:xxxxxxx

    I have no idea how I would go about doing this. It would really save me time if it was possible to code a small script that automatically does this everytime I upload a txt file. Any help would be greatly appreciated!

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you should at least tell us what server side language that you use....

    below code will read in the file, and then for each line, it will split the value into pieces on the ':' (can be 0, 1 or more pieces).
    it will then insert a new record for each piece.
    PHP Code:
    $lines file('your_file.txt');

    foreach (
    $lines as $line) {
       
    $arr_lines_insert explode(':'$line);
       foreach (
    $arr_lines_insert  as $line_insert){
           
    $sql "INSERT INTO your_table (your_column) VALUES('"$line_insert ."')"
           
    mysql_query($sql) or die('Queryproblem inserting line');
       }

    where i used "your_" you need to replace this with your file/table/column
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •