Jump to content

txt update


Eralmidia

Recommended Posts

Hi

I got this script that reads a textfile line by line and compares it to a searchstring from the user. If a match is found the line will be echoed to screen for editing ie in a textarea. Multiple searchresults may occur. Hvor can get the updated data back into the string which contains all the data? In other words, how do I update the txt contents?

 

Thanks in advance :)

Link to comment
https://forums.phpfreaks.com/topic/41415-txt-update/
Share on other sites

Erm, one way would be to remember what line you are editing.

 

Then when the user submits the form, you loop through the file copying the lines until you reach the edited line. Then insert the edited line, then continue copying lines from the file.

 

Then delete the original and rename the new file.

 

http://www.phpit.net/article/beginners-introduction-php-file-functions/

 

But this is quite complicated. There may be an easier way if you think about it.

 

monk.e.boy

Link to comment
https://forums.phpfreaks.com/topic/41415-txt-update/#findComment-200621
Share on other sites

well i guess u could simlify the same loop: when walking through the txt file the first time (for the search results) a counter counting through the rows a s u go along. first time u reach a match, copy all previous lines into a variable or a new txt file (copy line 0 to line X-1 where x is the line number of the matched text). then set counter back to 0, and start counitng again, each time feeding the text blocks into elements of any array or something. then when you want to put the file back together with edited text, do a foreach loop like this and implode.:

foreach ($textBlockArray as $v){
            $v = $v.$correspondingEditedTextLine;
}
$newtext = implode("", $textBlockArray);
fopen($filename, "w"); //$filename can be old file because "w" mode overwrites old contents

Link to comment
https://forums.phpfreaks.com/topic/41415-txt-update/#findComment-200646
Share on other sites

Lol. Actually, im studying to become a teacher :P BUT, i'll make that the students learn the right things :P Think i'll just try out some array functions and some loops after reading the file contents with file().. or something like that :P the really brilliant part of the assigment is the fact that we use a textfile to "make things easier".. and SELECT became hard to understand when?

Link to comment
https://forums.phpfreaks.com/topic/41415-txt-update/#findComment-200728
Share on other sites

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.