SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2001
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP fwrite() question

    I am using a form to write to a text file with PHP. What would I do to write each new set of form data *above* the previous one? This is because I am making a simple news posting system and I want to make each news item above the previous one, and can't work with a database to accomplish it

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I assume that you are using a system where each record is stored in the text file one per line. If this is the case why not add an extra field with a unixtime stamp in it, then you can just read all the records into an array susing the timestamp as the key, then you can reverse sort the array and presto you get all the records in time order without worrying about how they are stored in the text file.

    See this example:

    data.txt
    Code:
    Coolio finds rap|me|986509605
    I like reggae|me|995609605
    My Sister likes rock|me|996609205
    Yo read this|me|991609605
    Red Worm Sucks|me|986609605
    php script to retrieve the entries in date descending order.
    PHP Code:
    <?
    /*
    Grab each line form the text file and store
    it in the array $data
    */
    $data file("data.txt");

    /*
    Loop through each element of $data and make a new array
    $array with the key being the time from the text file
    */
    foreach($data as $key => $val) {
        
    $tmp explode("|"$val);
        
    $array[$tmp[2]] = array("title" => $tmp[0],
                                
    "author" => $tmp[1]
                                );
        }

    //Sort the array in reverse according to the key.
    krsort($array);
    foreach(
    $array as $key => $val) {
        print 
    $array[$key]["title"]." ".$array[$key]["title"]." ".date("m-d-Y"$key)."<br>";
        }    
    ?>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or you can just do $data = file("output.txt"); $data = array_reverse($data);
    ck :: bringing chris to the masses.

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could do that, and it should work fine for most cases. However if someone ever edited your text file things could get messy. And it would probably be best to store a time when the headline was added anyway, if you are going to do that then you would never have to worry about the text file getting changed or the order in which the headlines are stored in the text file. But Anarchos' solution would work for most cases.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2001
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, freddy, I'm a very beginner PHP scripter so I don't know how to use arrays. Here's what my code looked like at first:

    PHP Code:
    <?

    $filename
    ="/web/sites/126/tgs3c/www.tgs3c.f2s.com/bin/news/data.php";

    $today getdate(); 
    $minutes $today['minutes'];
    $hours $today['hours'];
    $weekday $today['weekday'];
    $month $today['month']; 
    $mday $today['mday']; 
    $year $today['year']; 

    $output_weekday "<table class=\"table02\"><tr><td class=\"td01\">" $weekday;
    $output_month ",&nbsp;" $month;
    $output_mday "&nbsp;" $mday;
    $output_year ",&nbsp;" $year;
    $output_hours "&nbsp;-&nbsp;" $hours;
    $output_minutes $minutes;
    $output_title "&nbsp;CDT&nbsp;-&nbsp;" $title;
    $output_message "</td></tr><tr><td class=\"td03\">" $message;
    $output_footer "</td></tr></table><br><br>";



    if (!
    file_exists($filename)) {
    $fp fopen($filename"w+");
    fwrite($fp,$output_weekday);
    fwrite($fp,$output_month);
    fwrite($fp,$output_mday);
    fwrite($fp,$output_year);
    fwrite($fp,$output_hours);
    fwrite($fp,$output_minutes);
    fwrite($fp,$output_title);
    fwrite($fp,$output_message);
    fwrite($fp,$output_footer);
    fclose($fp);
    }

    echo 
    "News submitted.";
    ?>
    But that didn't work at all, most likely something to do with my choice of file opening mode. I'll try your example, though, freddy.


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
  •