Results 1 to 8 of 8
  1. #1
    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());

    My_table structure contains columns "id" and "name"

    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

  2. #2
    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()); 

    Regards
    www.hostlatch.com
    SDD Hosting - SSD Reseller - Premium VPS
    Premium UK Network - Instant Setup

  3. #3
    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 ???

  4. #4
    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 ||

  5. #5
    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

  6. #6
    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());  


  7. #7
    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

  8. #8
    Join Date
    May 2003
    Posts
    852
    Yes all the text files contain the same number of records

Similar Threads

  1. PHP nested foreach loop
    By Dbwieler in forum Programming Discussion
    Replies: 4
    Last Post: 09-11-2013, 02:15 PM
  2. need help with php/mysql foreach loop
    By mjfroggy in forum Programming Discussion
    Replies: 11
    Last Post: 07-22-2009, 09:39 AM
  3. Using Foreach/Loop on a query
    By acctman in forum Programming Discussion
    Replies: 5
    Last Post: 09-02-2008, 11:05 AM
  4. Problem with foreach loop
    By P-nut in forum Programming Discussion
    Replies: 4
    Last Post: 08-09-2007, 09:43 PM
  5. using a foreach() loop
    By mjfroggy in forum Programming Discussion
    Replies: 8
    Last Post: 06-23-2007, 01:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •