Web Analytics Made Easy -
StatCounter displaying/adding data using a text file - CodingForum

Announcement

Collapse
No announcement yet.

displaying/adding data using a text file

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • displaying/adding data using a text file

    hey everyone

    I have a little news section on my website, which functions alot like a guestbook (except its all password protected).

    To add the data i simply do...

    <?php

    $entry = $_POST['entry'];
    $file = "text.txt";
    $do = fopen($file, "a") or die("Error");
    $bytes = fwrite($do, $entry);
    echo "Data successfully added. ".$bytes." bytes added.";

    ?>

    When I then call upon that data and display it all however, the most recent news is displayed below the older entries. How can I reverse the order so that the newer posts come above the old ones?

    Thanks

    Jay Vincent
    JayV

  • #2
    Write the code in reverse

    ;['yrtne']TSOP_$ = yrtne$

    That should do it.


    Ok maybe not. We would need to see the code you are using to read in the text file.
    Spookster
    CodingForum Supreme Overlord
    All Hail Spookster

    Comment


    • #3
      OK:

      $title = $_POST['title'];
      $news = $_POST['news'];
      $date = $_POST['date'];
      $entry = "<table width=500 cellpadding=3 cellspacing=1 border=1 bordercolor=#404040><tr><td width=500 height=30 bgcolor=#FF00FF><font color=white><i>".$date."</i></font></td></tr><td width=500 bgcolor=#FF80FF valign=top><font color=white><b>".$title."</b><br>".$news."<p></font></td></tr></table><p>";

      $entry is then written into the text file, then i simply use include("file.txt"); to display the entries.
      JayV

      Comment


      • #4
        Oh ok. I figured you were opening the file and reading the contents in before outputing it to the page. If you are just using it as an include file then you will need to write the entries in reverse order. You will need to figure out what to use as a delimiter because you will need to read the entries in and then add the newest entry to the beginning and then write them all back to the file. The delimiter would allow PHP to determine where one entry ends and the next one begins.
        Spookster
        CodingForum Supreme Overlord
        All Hail Spookster

        Comment


        • #5
          I'd read the file in, splitting into an array based on delimeter, and print in reverse.

          Comment


          • #6
            $file = "guestbook.data";
            $do = fopen($file, "r");
            $data = fread($do, filesize($file));
            fclose($do);
            $delimiter = ";";
            $splitdata = explode($delimiter, $data);
            $counter = "";
            foreach ( $splitdata as $entry )
            {
            $counter = $counter+1;
            echo $entry;
            }

            Thats what I've got, but I dont know how to print in reverse. Any help would be appreciated
            JayV

            Comment


            • #7
              PHP Code:
              <?
              $splitdata 
              array_reverseexplode($delimiter$data) ) ; 
              ?>
              resistance is...

              MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

              Comment


              • #8
                Thanks Mr Moderator Man, that works a treat

                Thanks again
                JayV

                Comment

                Working...
                X