3raser Posted July 27, 2010 Share Posted July 27, 2010 Alright, I have a file that contains about 13,000 items. And an example is this: 0 - Dwarf remains 1 - Toolkit 2 - Cannonball 3 - Nulodion's notes 4 - Ammo mould 5 - Instruction manual 6 - Cannon base 799 - Not Existing 7 - Cannon base 8 - Cannon stand 9 - Cannon stand 10 - Cannon barrels 11 - Cannon barrels 12 - Cannon furnace 13 - Cannon furnace 14 - Railing 15 - Holy table napkin 16 - Magic whistle 17 - Grail bell 18 - Magic gold feather 19 - Holy grail 20 - White cog 21 - Black cog How can I make it so this list can be added into the database, using two fields - id, name I've asked this question before, but I haven't got an answer. Link to comment https://forums.phpfreaks.com/topic/208974-how-do-i-do-this/ Share on other sites More sharing options...
jcbones Posted July 27, 2010 Share Posted July 27, 2010 include 'db_connect.php'; $filename = 'text.txt'; $db_table = 'my_table'; $file = file($filename); foreach($file as $line) { $ex = explode('-',$line); $id = trim($ex[0]); $name = trim($ex[1]); $sql = "INSERT INTO $db_table VALUES ('$id','$name')"; mysql_query($sql); } Something like that should get it done. There are other ways to do it, that would be faster. This is just a simplistic example. Link to comment https://forums.phpfreaks.com/topic/208974-how-do-i-do-this/#findComment-1091534 Share on other sites More sharing options...
3raser Posted July 27, 2010 Author Share Posted July 27, 2010 include 'db_connect.php'; $filename = 'text.txt'; $db_table = 'my_table'; $file = file($filename); foreach($file as $line) { $ex = explode('-',$line); $id = trim($ex[0]); $name = trim($ex[1]); $sql = "INSERT INTO $db_table VALUES ('$id','$name')"; mysql_query($sql); } Something like that should get it done. There are other ways to do it, that would be faster. This is just a simplistic example. Could you add comments explaining what each line is doing? Link to comment https://forums.phpfreaks.com/topic/208974-how-do-i-do-this/#findComment-1091538 Share on other sites More sharing options...
jcbones Posted July 27, 2010 Share Posted July 27, 2010 include 'db_connect.php'; //include database connection info. $filename = 'text.txt'; //name of the text file. $db_table = 'my_table'; //name of your database table. $file = file($filename); //get contents of the text file in an array. foreach($file as $line) { //perform the following actions for each line in the text file. $ex = explode('-',$line); //split the line on the dash $id = trim($ex[0]); //trim the value before the dash, this is the id. $name = trim($ex[1]); //trim the value after the dash, this is the name. $sql = "INSERT INTO $db_table VALUES ('$id','$name')"; //put the data into a query. mysql_query($sql); //send it to the database. } Link to comment https://forums.phpfreaks.com/topic/208974-how-do-i-do-this/#findComment-1091552 Share on other sites More sharing options...
3raser Posted July 27, 2010 Author Share Posted July 27, 2010 Thank you, very much appreciated. I'll try it. I just needed the comment because I wasn't exactly sure on what some of the things were doing. VERY appreciated. Link to comment https://forums.phpfreaks.com/topic/208974-how-do-i-do-this/#findComment-1091555 Share on other sites More sharing options...
3raser Posted July 27, 2010 Author Share Posted July 27, 2010 include 'db_connect.php'; //include database connection info. $filename = 'text.txt'; //name of the text file. $db_table = 'my_table'; //name of your database table. $file = file($filename); //get contents of the text file in an array. foreach($file as $line) { //perform the following actions for each line in the text file. $ex = explode('-',$line); //split the line on the dash $id = trim($ex[0]); //trim the value before the dash, this is the id. $name = trim($ex[1]); //trim the value after the dash, this is the name. $sql = "INSERT INTO $db_table VALUES ('$id','$name')"; //put the data into a query. mysql_query($sql); //send it to the database. } THANK YOU. IT WORKED. THANK YOU! I'VE BEEN TRYING TO DO THIS FOREVER!!!! THANK YOU! Link to comment https://forums.phpfreaks.com/topic/208974-how-do-i-do-this/#findComment-1091556 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.