dave_biscuits Posted August 17, 2009 Share Posted August 17, 2009 Hi everyone. Got an issue with a bit of code that I can't figure out: User submits the page with data to save. PHP reads a text file of saved data, updates the data with the changes, writes it back to the text file, then finally re-reads the text file in order to show on screen that the changes have definitely been written properly. The initial read and write works fine - it does definitely save it to the text file no problems - except the re-read after the write doesnt work properly - it keeps displaying on screen the data from the very first read (before changes were made). The result is I have to always refresh the page to get it to read the file again and display the changes properly. My only thought is that PHP only writes to the text file after the code has fully executed - rather than on the fly ... Code is below, any idea what I've done wrong or how I can get around this?? $my_array = ''; // If user submitted data, read, make changes, write new data string. if($_POST['dater']!='') { // READ CODE $my_array = ''; $filename = "../calendar.data"; if(filesize($filename)!='0') { $file = fopen($filename, 'r'); $theData = fread($file, filesize($filename)); fclose($file); $my_array = ''; $exploded = explode(',',$theData); foreach ($exploded as $explode) { if($explode!='') { $e2 = explode('#',$explode); $my_array[$e2[0]] = $e2[1]; } } } // INSERT NEW DATA $my_array[$_POST['dater']] = $_POST['status']; // WRITE CODE $imploded = ''; foreach($my_array as $key=>$val) { if($val!='') { $imploded = $imploded.rawurlencode($key)."#".rawurlencode($val).","; } } $filename = "../calendar.data"; $file = fopen($filename, 'w') or die("can't open file"); fwrite($file, $imploded); fclose($file); } // GO FOR MAIN READ // RE-READ DATA $booked_array = ''; $myFile = "../calendar.data"; if(filesize($myFile)!='0') { $fh = fopen($myFile, 'r'); $theData = fread($fh, filesize($myFile)); fclose($fh); $booked_array = ''; $exploded = explode(',',$theData); foreach ($exploded as $explode) { if($explode!='') { $e2 = explode('#',$explode); $booked_array[$e2[0]] = $e2[1]; } } } // **** Error: booked_array showing pre-changes array ... } Quote Link to comment https://forums.phpfreaks.com/topic/170581-fread-fwrite-code-help/ Share on other sites More sharing options...
oni-kun Posted August 17, 2009 Share Posted August 17, 2009 PHP uses output buffering and the script would complete before it'd be displayed after writing the file.... I'd recommend using ob_start, flush and sleep() to halt execution of the re-read, I'm not quite in tune with how you coded that.. I can't rewrite it to work for you. Quote Link to comment https://forums.phpfreaks.com/topic/170581-fread-fwrite-code-help/#findComment-899723 Share on other sites More sharing options...
dave_biscuits Posted August 17, 2009 Author Share Posted August 17, 2009 Thanks, I'll give that a go. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/170581-fread-fwrite-code-help/#findComment-899759 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.