...

View Full Version : Separating parts of data into different rows



sammy
08-04-2006, 12:01 AM
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!

raf
08-04-2006, 07:15 AM
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.


$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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum