Eralmidia Posted March 6, 2007 Share Posted March 6, 2007 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 More sharing options...
monk.e.boy Posted March 6, 2007 Share Posted March 6, 2007 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 More sharing options...
Eralmidia Posted March 6, 2007 Author Share Posted March 6, 2007 Yes, thats the theoretical solution i came up with too... but I was kinda hoping there was a better and easier way to do it? Link to comment https://forums.phpfreaks.com/topic/41415-txt-update/#findComment-200639 Share on other sites More sharing options...
iceman400 Posted March 6, 2007 Share Posted March 6, 2007 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 More sharing options...
monk.e.boy Posted March 6, 2007 Share Posted March 6, 2007 Yes, thats the theoretical solution i came up with too... but I was kinda hoping there was a better and easier way to do it? Why are the users searching a file, then editing one line? Maybe there is a better solution. monk.e.boy Link to comment https://forums.phpfreaks.com/topic/41415-txt-update/#findComment-200654 Share on other sites More sharing options...
Eralmidia Posted March 6, 2007 Author Share Posted March 6, 2007 Its suppose to be a admin kinda thing.. it searches the lines containing names, address and phone number in a textfile, and the admin is allowed to edit or delete the data. I know that a db is the solution here, but this is an assignment and we are told to use a textfile Link to comment https://forums.phpfreaks.com/topic/41415-txt-update/#findComment-200667 Share on other sites More sharing options...
monk.e.boy Posted March 6, 2007 Share Posted March 6, 2007 Ugh, why set an assignment teaching the *wrong* way to do something?! That's why teachers teach, can't get a real job ;) ;D Like you say, it should use a DB. There's a reason why IBM Oricle MySQL are household names. We all got over flat files 25 years ago :D monk.e.boy Link to comment https://forums.phpfreaks.com/topic/41415-txt-update/#findComment-200714 Share on other sites More sharing options...
Eralmidia Posted March 6, 2007 Author Share Posted March 6, 2007 Lol. Actually, im studying to become a teacher BUT, i'll make that the students learn the right things Think i'll just try out some array functions and some loops after reading the file contents with file().. or something like that 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 More sharing options...
Eralmidia Posted March 6, 2007 Author Share Posted March 6, 2007 make sure even Link to comment https://forums.phpfreaks.com/topic/41415-txt-update/#findComment-200730 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.