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
https://forums.phpfreaks.com/topic/252932-write-a-loop-to-a-file/
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?

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.