Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Inverse a

  1. #1
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default Inverse a

    In the fopen($File, 'a'); function is there a way to have it write at the start of the file rather then the end; so newest things being added would appear first?
    This is my current code to clarify a bit what I'm doing maybe. Maybe this won't help at all.

    PHP Code:
    $File "questions.txt";
    $Handle fopen($File'a');
    $Data "<p>From:" $visitor .  "<br />Submitted on:" $todayis "<br />" $notes "</p><hr />";
    fwrite($Handle$Data);
    fclose($Handle); 
    Last edited by bluewalrus; 12-14-2008 at 05:27 PM.

  2. #2
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    Instead of a, try using r+.

  3. #3
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    That overwrites whatever is at the start

  4. #4
    Join Date
    Jul 2007
    Location
    Irmo, SC
    Posts
    96
    Thanks
    10
    Thanked 7 Times in 7 Posts

    Default

    possibly try traversing an array of the file, adding elements prior to the start, then rewriting it?

  5. #5
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Don't really get what you mean?

  6. #6
    Join Date
    Jul 2007
    Location
    Irmo, SC
    Posts
    96
    Thanks
    10
    Thanked 7 Times in 7 Posts

    Default

    i havent actually tried it so I can not give you a code example. but you should be able to load your file into an array, then sort the array in reverse order. add your line to the array which making it higher in element. then write the array back to the file.

    you would probably need to crush the file when writing it back. ie deleting the content and rewriting it.

  7. #7
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    hmmm I've never used arrays in php and haven't used an arrays in general in a while. You know of any tutorials?

  8. #8
    Join Date
    Jul 2007
    Location
    Irmo, SC
    Posts
    96
    Thanks
    10
    Thanked 7 Times in 7 Posts

    Default

    try googling it. you will have to sort through a bunch of invalid examples etc.

    $fp = @fopen($filename, 'r');
    if ($fp) {
    $array = explode("\n", fread($fp, filesize($filename)));
    }

    that will load a file into an array.

    while (list($key,$value) = each($array)) {
    echo "$key : $value<br>";

    this should show you if you have values in the array after reading the file

    once you have that working... you could then use rsort function or something else to sort the array in reverse order. stick whatever you want in the array as the highest element and then write it back to the file.

    the above code is not mine, something i found on google pretty quickly. i dont know if it works but at a glance seems syntactically ok

    pretty good article.

    http://articles.techrepublic.com.com...1-5077722.html

  9. The Following User Says Thank You to gpigate For This Useful Post:

    bluewalrus (12-05-2008)

  10. #9
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Ahh I've figured it out thanks.

    For anyone else that is searching....
    PHP Code:
    $File "textfilename.txt";
    $Handle fopen($File"r+");
    $Data "WHATEVER YOU WANT TO BE INSERTED";
    if (
    $Data <> "")
    {    
        
    $existingText file_get_contents($File);
        
    fwrite($Handle$Data $existingText "\n");
    }
    fclose($Handle); 

  11. #10
    Join Date
    Jul 2007
    Location
    Irmo, SC
    Posts
    96
    Thanks
    10
    Thanked 7 Times in 7 Posts

    Default

    but reverse traversing an array and launching a lunar lander would have been much more fun and rewarding

    glad you figured it out.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •