Jump to content

Write a loop to a file


Samuz

Recommended Posts

Got a little stuck here.

 

I'm trying to format an existing .txt file, format it and then write it to .sql file.

 

Here's my code:

 

$handle = @fopen("nations.txt", "r");
if ($handle) {
    while (($buffer = fgets($handle, 1024)) !== false) {

                      $column = explode("|",$buffer); // format file
                        $myFile = "nation_dump_".date("Y-m-d").".sql"; // create file
					$fh = fopen($myFile, 'w') or die("can't open file");

					foreach($column as $col)
					{
					fwrite($fh, $col[0]); //write the first broken segment from explode() to file
					}
					fclose($fh);
    }
    if (!feof($handle)) {
        echo "Error: unexpected fgets() fail\n";
    }
    fclose($handle);
}

 

The error is on this line, within the foreach loop.

fwrite($fh, $col[0]);

 

When I removed the index it only wrote the last line to the file, but I want it to loop through all the lines..

 

What am I doing wrong here?

Link to comment
Share on other sites

You are opening $myFile inside your while(){}, so the output file will be recreated each pass through the while loop. Only information from the last line of the input file will remain in the output file.  You should only open the output file once, before the start of the loop.

 

When you explode each line from the input file, $column is an array of the elements in that line. Using a foreach(){} loop on $column, will iterate over each element in each line. $col is the (string) content of each element. Accessing $col[0] is actually accessing the first character of the content of each element. If you are just trying to write the first element of $column to the output file, you would not use a foreach(){} loop, you would just use $column[0].

 

What exact part of each input line are you trying to write to the output file?

Link to comment
Share on other sites

You are opening $myFile inside your while(){}, so the output file will be recreated each pass through the while loop. Only information from the last line of the input file will remain in the output file.  You should only open the output file once, before the start of the loop.

 

When you explode each line from the input file, $column is an array of the elements in that line. Using a foreach(){} loop on $column, will iterate over each element in each line. $col is the (string) content of each element. Accessing $col[0] is actually accessing the first character of the content of each element. If you are just trying to write the first element of $column to the output file, you would not use a foreach(){} loop, you would just use $column[0].

 

What exact part of each input line are you trying to write to the output file?

Thank you for your fast reply. I just took out the foreach loop and access the $column array directly.

 

Working as should now thanks!

Link to comment
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.