gigantorTRON Posted August 21, 2007 Share Posted August 21, 2007 Hello all! I'm having a hell of a time with an issue here... I have a page I'm coding for batch updates from a tab delimited file. Each row has data that has to be checked against and inserted/updated on different tables in my mySQL database. How would I go about parsing through the tab delimited file and then storing data between the tabs in order to insert individually into separate tables?? Here's what I have so far... $contents = file('/myfile.txt'); for ($i=0; $i<sizeof($contents); $i++) { $line = trim($contents[$i]); $arr = explode("\t", $line); $line[$i] = array('ID' => $arr[0], 'Record_Type' => $arr[1], 'First_Name' => $arr[2], 'MI' => $arr[3], 'Last_Name' => $arr[4], 'Firm' => $arr[5], 'Address' => $arr[6], 'type' => rtrim($arr[13])); I need to take the different columns.. ID, Record_Type, etc. and update different tables with each value where the IDs match. I'm a bit of a php n00b. Thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/65995-solved-multiple-table-insert-from-tab-delimited-file/ Share on other sites More sharing options...
NArc0t1c Posted August 21, 2007 Share Posted August 21, 2007 Hello there, Try the following script, <?php // We get the file as an array here. $Contents = file('File.txt'); // Loop trough the file using foreach, giving us single lines. foreach($Contents as $Lines){ list($ID, $Record_Type, $First_Name, $MI, $Last_Name, $Firm, $Address, $Type) = explode('delimiter', $Lines); $Query = 'UPDATE Data...'; mysql_query($Query); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/65995-solved-multiple-table-insert-from-tab-delimited-file/#findComment-330007 Share on other sites More sharing options...
gigantorTRON Posted August 21, 2007 Author Share Posted August 21, 2007 Thanks for the reply! That's about where I've gotten to in the meantime. If I were able to do a straight update it wouldn't be so difficult but I need to compare ID keys first. For example: if ($myTextArray["id"] = myTable.id) then UPDATE query Else (if !($myTextArray["id"] = myTable.id)) then run INSERT query any more insight?? I'm having trouble comparing the id values. Do I need to do a select query to get all of the id values from mySQL table?? Quote Link to comment https://forums.phpfreaks.com/topic/65995-solved-multiple-table-insert-from-tab-delimited-file/#findComment-330022 Share on other sites More sharing options...
NArc0t1c Posted August 21, 2007 Share Posted August 21, 2007 uhm.., Okay.. Try this, <?php $mq = mysql_connect('localhost', 'root', ''); mysqlselectdb('database', $mq); $Contents = file('File.txt'); $Query = mysql_query('SELECT ID FROM Table'); //Loop trough the table. while ($Row = mysql_fetch_array($Query)) { // Loop trough the file using foreach, giving us single lines. foreach ($Contents as $Lines) { list($ID, $Record_Type, $First_Name, $MI, $Last_Name, $Firm, $Address, $Type) = explode('delimiter', $Lines); if ($ID == $Row['ID']) { $Query = 'UPDATE Data...'; mysql_query($Query); } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/65995-solved-multiple-table-insert-from-tab-delimited-file/#findComment-330030 Share on other sites More sharing options...
gigantorTRON Posted August 21, 2007 Author Share Posted August 21, 2007 Thanks, this definitely helps. However I need the looping to go the "other way." The contents of the read-in file are much smaller than the contents of the table. I need to match the id from the file to the id of the table and not vice versa. Any more help?? Quote Link to comment https://forums.phpfreaks.com/topic/65995-solved-multiple-table-insert-from-tab-delimited-file/#findComment-330152 Share on other sites More sharing options...
NArc0t1c Posted August 21, 2007 Share Posted August 21, 2007 oh.. Use this then, <?php $mq = mysql_connect('localhost', 'root', ''); mysqlselectdb('database', $mq); $Contents = file('File.txt'); $Query = mysql_query('SELECT ID FROM Table'); //Loop trough the table. $Row = mysql_fetch_array($Query); // Loop trough the file using foreach, giving us single lines. foreach ($Contents as $Lines) { list($ID, $Record_Type, $First_Name, $MI, $Last_Name, $Firm, $Address, $Type) = explode('delimiter', $Lines); if ($ID == $Row['ID']) { $Query = 'UPDATE Data...'; mysql_query($Query); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/65995-solved-multiple-table-insert-from-tab-delimited-file/#findComment-330218 Share on other sites More sharing options...
gigantorTRON Posted August 21, 2007 Author Share Posted August 21, 2007 Thanks again, but it looks like this is comparing line by line. I need the script to take a value from the text file and compare it to ALL values for id on the table... ??? Quote Link to comment https://forums.phpfreaks.com/topic/65995-solved-multiple-table-insert-from-tab-delimited-file/#findComment-330330 Share on other sites More sharing options...
gigantorTRON Posted August 21, 2007 Author Share Posted August 21, 2007 Anymore help here?? I'm dyin!! Quote Link to comment https://forums.phpfreaks.com/topic/65995-solved-multiple-table-insert-from-tab-delimited-file/#findComment-330370 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.