Results 1 to 8 of 8
-
10-14-2014, 08:27 AM #1Web Hosting Master
- Join Date
- May 2003
- Posts
- 852
Help with foreach loop from text file
Hi,
I have text file "file.txt" contains data like
123
1234
12547
2541
around 100 lines
I am inserting each line to SQL table under one column with name "id" via php code and it works perfect
PHP Code:mysql_connect("localhost", "username", "password");
mysql_select_db("my_database");
$data = file_get_contents("file.txt");
$lines = explode("\n", $data); // array by line
foreach($lines as $line){
$line = trim($line); //Removes ending line break
mysql_query("INSERT INTO `my_table` ( `id` ) VALUES ( '$line' )") or die(mysql_error());
}
So if I have another text file "file2.txt" which contains data like
Boob
Paul
Sara
Michael
Whenever I use same script above, it start inserting in "name" column where the first "id" column is empty
The table become like this
id name
123 empty
1234 empty
12547 empty
2541 empty
empty Boob
empty Paul
empty Sara
empty Michael
How to fix this and make the table look like this
id name
123 Boob
1234 Paul
12547 Sara
2541 Michael
Regards
-
10-14-2014, 08:41 AM #2WHT Addict
- Join Date
- Aug 2009
- Posts
- 118
Hi, you need insert both pieces of data at the same time. Try something like this (make sure to check for typos)...
PHP Code:mysql_connect("localhost", "username", "password");
mysql_select_db("my_database");
$data[0] = file_get_contents("file.txt");
$ids = explode("\n", $data[0]); // array by line
$data[1] = file_get_contents("file2.txt");
$keys = explode("\n", $data[1]);
foreach(array_combine($data[0], $data[1]) as $id => $value){
$id = trim($id);
$value = trim($value); //Removes ending line break
mysql_query("INSERT INTO `my_table` ( `id`, `name` ) VALUES ( '$id', `$value` )") or die(mysql_error());
}
██ www.hostlatch.com
██ SDD Hosting - SSD Reseller - Premium VPS
██ Premium UK Network - Instant Setup
-
10-14-2014, 02:26 PM #3Web Hosting Master
- Join Date
- May 2003
- Posts
- 852
That worked for two columns
What if I have six colums ?
Id, name, age, job, sex, email
With diffrent six text files
file1-6.txt ???
-
10-14-2014, 02:30 PM #4Web Hosting Master
- Join Date
- May 2011
- Location
- /root
- Posts
- 630
Yes, you can work on the same logic if you have 6 different files. @HL_James . Great tip
|| Tecsys Solutions LLC | Outperforming the Performers!! ||
|| Outsourced Server Management and Technical Support Solutions ||
|| Now Offering Secure Managed VPS and Dedicated Servers specially setup for Hosting Providers ||
|| https://www.24x7TechnicalSupport.net || https://www.mxv.net ||
-
10-15-2014, 02:42 AM #5WHT Addict
- Join Date
- Aug 2009
- Posts
- 118
Yeah you can easily use the same thought pattern to insert all the data.
Another thing, I don't know where you're using this script. But mysql_connect and its related functions have now been depreciated so it would be wise to use mysqli or PDO.
Regards██ www.hostlatch.com
██ SDD Hosting - SSD Reseller - Premium VPS
██ Premium UK Network - Instant Setup
-
10-15-2014, 05:06 AM #6Web Hosting Master
- Join Date
- May 2003
- Posts
- 852
I used this code but it's not working
I don't know how to set $variables after "as" for additional data
PHP Code:
mysql_connect("localhost", "username", "password");
mysql_select_db("my_database");
$data[0] = file_get_contents("file0.txt");
$ids = explode("\n", $data[0]); // array by line
$data[1] = file_get_contents("file1.txt");
$keys = explode("\n", $data[1]);
$data[2] = file_get_contents("file2.txt");
$keys = explode("\n", $data[2]);
$data[3] = file_get_contents("file3.txt");
$keys = explode("\n", $data[3]);
$data[4] = file_get_contents("file4.txt");
$keys = explode("\n", $data[4]);
$data[5] = file_get_contents("file5.txt");
$keys = explode("\n", $data[5]);
foreach(array_combine($data[0], $data[1], $data[2], $data[3], $data[4], $data[5]) as $id => $value){
$id = trim($id);
$value = trim($value); //Removes ending line break
mysql_query("INSERT INTO `my_table` ( `id`, `name`, ...... ) VALUES ( '$id', `$value` )") or die(mysql_error());
}
-
10-15-2014, 05:41 AM #7WHT Addict
- Join Date
- Aug 2009
- Posts
- 118
Actually you can't use array_combine() in this way, see here for more info: http://php.net/manual/en/function.array-combine.php
Do all the text files contain the same number of records?
Regards██ www.hostlatch.com
██ SDD Hosting - SSD Reseller - Premium VPS
██ Premium UK Network - Instant Setup
-
10-15-2014, 06:01 AM #8Web Hosting Master
- Join Date
- May 2003
- Posts
- 852
Yes all the text files contain the same number of records
Similar Threads
-
PHP nested foreach loop
By Dbwieler in forum Programming DiscussionReplies: 4Last Post: 09-11-2013, 02:15 PM -
need help with php/mysql foreach loop
By mjfroggy in forum Programming DiscussionReplies: 11Last Post: 07-22-2009, 09:39 AM -
Using Foreach/Loop on a query
By acctman in forum Programming DiscussionReplies: 5Last Post: 09-02-2008, 11:05 AM -
Problem with foreach loop
By P-nut in forum Programming DiscussionReplies: 4Last Post: 08-09-2007, 09:43 PM -
using a foreach() loop
By mjfroggy in forum Programming DiscussionReplies: 8Last Post: 06-23-2007, 01:24 PM