jcbarr Posted July 27, 2006 Share Posted July 27, 2006 Here is the code.[code] // Write players from team 1 $handle = fopen("trades.txt", "a"); $content = "$team1 send:\n"; fwrite($handle, $content); if ($player1) { $sql = "SELECT * FROM batrating WHERE PID='$player1'"; $query = mysql_query($sql); $a = mysql_fetch_array($query); $pos = $a['pos']; $fname = $a['firsname']; $lname = $a['lastname']; $content = "$pos $fname $lname\n"; fwrite($handle, $content); } [/code]I have opened the trades.txt and placed the cursor at the bottom and made it writeable. My question is this, is there any way for me to pen that text file and write the new data to the top of the text file without overwriting the data below it?The script prints the players traded to the text file, and the way that it is right now it writes the newest trades at the bottom so you have to scroll down to see them. I want to be able to open the text file and see the newest trades at the top. Any help would be great. Quote Link to comment https://forums.phpfreaks.com/topic/15764-writing-new-data-to-the-top-of-a-text-file/ Share on other sites More sharing options...
kenrbnsn Posted July 27, 2006 Share Posted July 27, 2006 Use the file() function to read the file into an array.Reverse the array.Add the new line to the bottom of the array.Reverse the array.Write the new file.Ken Quote Link to comment https://forums.phpfreaks.com/topic/15764-writing-new-data-to-the-top-of-a-text-file/#findComment-64444 Share on other sites More sharing options...
jcbarr Posted July 27, 2006 Author Share Posted July 27, 2006 I don't know too much about arrays, but I'm reading up on it right now. array_reverse and all that stuff.I just went the easy route, dumped the contents to a variable and then open the file, truncate it, write the new data, then dump the contents back in at the bottom.Seems to be working, and the file shouldn't ever get that big to pose any problems.Thanks for the help and pointing me in that direction. Quote Link to comment https://forums.phpfreaks.com/topic/15764-writing-new-data-to-the-top-of-a-text-file/#findComment-64495 Share on other sites More sharing options...
Ifa Posted July 27, 2006 Share Posted July 27, 2006 Can't he just use r+ in the fopen? Quote Link to comment https://forums.phpfreaks.com/topic/15764-writing-new-data-to-the-top-of-a-text-file/#findComment-64510 Share on other sites More sharing options...
jcbarr Posted July 27, 2006 Author Share Posted July 27, 2006 No, it will overwrite what is already there. I tried that. Quote Link to comment https://forums.phpfreaks.com/topic/15764-writing-new-data-to-the-top-of-a-text-file/#findComment-64599 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.