I seemed to have a problem to try update my data kept in a textfile with the following code:
$filename = "months.txt";
$rowToUpdate = 2;
$newString = "This text is updated
$arrFp = file( $filename );
$arrFp[$rowToUpdate] = $newString;
$numLines = count( $arrFp );
$fp = fopen( $filename, "w" );
for($i=0; $i<$numLines; $i++)
fclose( $fp );
My textfile simply contained:
What happened is that, everytime the codes are executed, it would update the row, then it would leave any rows after that blank in my textfile, like:
This text is updated.
(then blank the rest of the file)
What could be the problem? Theoretically, it should be able to loop pass the row to be updated, and just re-write the original values into the file, right?
Please help! Thanks in advance!