Jump to content

Writing new data to the top of a text file


jcbarr

Recommended Posts

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.
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.

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.