Samuz Posted December 11, 2011 Share Posted December 11, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/252932-write-a-loop-to-a-file/ Share on other sites More sharing options...
PFMaBiSmAd Posted December 11, 2011 Share Posted December 11, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/252932-write-a-loop-to-a-file/#findComment-1296740 Share on other sites More sharing options...
Samuz Posted December 11, 2011 Author Share Posted December 11, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/252932-write-a-loop-to-a-file/#findComment-1296749 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.