Jump to content

Recommended Posts

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

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

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

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.