Reputation: 449
I have a major text file that I need to import into a database, but I honestly have no idea how to. I've tried some exploding, but I ended up dead.
The format of the file is on the bottom of this post. ID, name, members, lendable and such are column names. All information between the ---'s are part of one row. Can anybody push me towards the right direction to help me import this?
-------------------------------
ID: 0
Name: Dwarf remains
Members: true
Lendable: false
Stackable: false
Shop value: 0
Interface model: 2595
Model position x: -4
Model position y: 12
Model rotation x: 436
Model rotation y: 320
Model rotation z: 0
Model zoom: 1780
Interface options: [null, null, null, null, Destroy]
Ground options: [null, null, null, null, null]
Release: 27 May 2003
Quest: Dwarf Cannon
Tradeable: No
Destroy: These are the remains of the dwarf who was stationed in the Guard Tower.
Examine: The body of a Dwarf savaged by Goblins.
Weight: Unknown
-------------------------------
ID: 1
Name: Toolkit
Members: true
Lendable: false
Stackable: false
Shop value: 0
Interface model: 2679
Model position x: -3
Model position y: 2
Model rotation x: 2026
Model rotation y: 477
Model rotation z: 0
Model zoom: 1164
Interface options: [null, null, null, null, Destroy]
Ground options: [null, null, null, null, null]
Release: 27 May 2003
Quest: Dwarf Cannon
Tradeable: No
High alch: 0 coins
Low alch: 0 coins
Destroy: I got this from Captain Lawgof.
Examine: Good for repairing a broken cannon.
Weight: 0 kg
-------------------------------
Upvotes: 0
Views: 70
Reputation: 7948
First off, obviously you need to call the file, then you could just use plain ol' foreach and build the array. Consider this example:
$contents = file('file1.txt');
$data = array();
$current_key = null;
foreach($contents as $key => $value) {
$value = explode(':', $value);
if(count($value) > 1) {
if($value[0] == 'ID') {
$current_key = $value[1];
$data[$current_key]['ID'] = trim($value[1]);
$columns[] = 'ID'; // prepare keys for insert
} else {
$data[$current_key][$value[0]] = trim($value[1]);
$columns[] = $value[0]; // prepare keys for insert
}
}
}
print_r($data);
// gather your keys for insert
$columns = array_unique($columns);
foreach($columns as $key => &$value) {
// make the columns look like columns
$value = preg_replace('/[\s]+/', '_', strtolower(trim($value)));
}
$columns = implode(', ', $columns);
// format values
$values = '';
foreach($data as $key => $value) {
$values .= "('" . implode("','", $value) . "'),";
}
// concatenate them (remove extra comma)
$statement = "INSERT INTO table ($columns) VALUES " . substr($values, 0, -1);
print_r($statement);
Sample Output:
Array
(
[0] => Array
(
[ID] => 0
[Name] => Dwarf remains
[Members] => true
[Lendable] => false
[Stackable] => false
[Shop value] => 0
[Interface model] => 2595
[Model position x] => -4
[Model position y] => 12
[Model rotation x] => 436
[Model rotation y] => 320
[Model rotation z] => 0
[Model zoom] => 1780
[Interface options] => [null, null, null, null, Destroy]
[Ground options] => [null, null, null, null, null]
[Release] => 27 May 2003
[Quest] => Dwarf Cannon
[Tradeable] => No
[Destroy] => These are the remains of the dwarf who was stationed in the Guard Tower.
[Examine] => The body of a Dwarf savaged by Goblins.
[Weight] => Unknown
)
[1] => Array
(
[ID] => 1
[Name] => Toolkit
[Members] => true
[Lendable] => false
[Stackable] => false
[Shop value] => 0
[Interface model] => 2679
[Model position x] => -3
[Model position y] => 2
[Model rotation x] => 2026
[Model rotation y] => 477
[Model rotation z] => 0
[Model zoom] => 1164
[Interface options] => [null, null, null, null, Destroy]
[Ground options] => [null, null, null, null, null]
[Release] => 27 May 2003
[Quest] => Dwarf Cannon
[Tradeable] => No
[High alch] => 0 coins
[Low alch] => 0 coins
[Destroy] => I got this from Captain Lawgof.
[Examine] => Good for repairing a broken cannon.
[Weight] => 0 kg
)
)
Sample Statement:
INSERT INTO table (id, name, members, lendable, stackable, shop_value, interface_model, model_position_x, model_position_y, model_rotation_x, model_rotation_y, model_rotation_z, model_zoom, interface_options, ground_options, release, quest, tradeable, destroy, examine, weight, high_alch, low_alch) VALUES ('0','Dwarf remains','true','false','false','0','2595','-4','12','436','320','0','1780','[null, null, null, null, Destroy]','[null, null, null, null, null]','27 May 2003','Dwarf Cannon','No','These are the remains of the dwarf who was stationed in the Guard Tower.','The body of a Dwarf savaged by Goblins.','Unknown'),('1','Toolkit','true','false','false','0','2679','-3','2','2026','477','0','1164','[null, null, null, null, Destroy]','[null, null, null, null, null]','27 May 2003','Dwarf Cannon','No','0 coins','0 coins','I got this from Captain Lawgof.','Good for repairing a broken cannon.','0 kg')
Important Note: You can use this as reference as I don't know what your table structure is, so the columns might be different.
Upvotes: 1