Hi, I have a Add News script on my site. Once the user that logged in submits what he wants to be on the site, it copies all of it into a .txt file.
I use http://www.tripod.lycos.co.uk as my server.
The problem is, when it wrights it into the .txt file, it removes the last thing that was written there. How would I make the text move down so the new text could be the first?
Code

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Adding</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<LINK REL=STYLESHEET TYPE="text/css" HREF="style.css">
<script src="fade.js" language="Javascript"></script>
</head>

<body bgcolor="#000000" alink="#BBBBBB" link="#BBBBBB" text="#DDDDDD" vlink="#BBBBBB">
<?php

$topic = stripslashes($topic);
$message = stripslashes($message);
$posted = " Posted By: Balrok";
$curr_news = file("news.txt");
$fontshit = "<font color=\"#666666\" size\"=-2\">";
$message = "<b>" . $fontshit . $topic . $posted . "</b><br>\n" . $message . "<p>\n";

foreach($curr_news as $value) {
   $message .= $value;
}


$fp = fopen("newsa.txt", "w");
fwrite($fp, $message);
fclose($fp);

echo "Success!";



?>


</body>
</html>

Thanks for your help

    Try this

    $fp = fopen("newsa.txt", "a");

    The "a" appends to the end of file on a write.

      I want the newest topic that was submitted to be on the top.. not the bottom.
      Unless that is what you mean..

        go to fopen from the manual and check out the options for modes.
        you are using 'w' for write and I believe you want to use 'a'
        (does that stand for append?) to write at the end... hope it helps...
        I see you got the answer before I chimed in.

          ok, I see that you guys have a clue what i'm talking about. What I want is the message that was just updated, to be the first thing you see, when you do a, it puts the new message that was just updated at the end, i want it at the begining..

            I don't know, but I'm sure if it's possible it's in the manual somewhere....

            Looking at your code, I'm not sure where you are inputting your new stuff to.

            But once you get it added to the file, you can look at how you are printing it out to your page.

            I think you might need to add a date or ID field to differentiate the posts so you can order them the way you'd like to.

            Like I said, I don't know, but good luck!

              i thinks the new lines are always appended at the end, thats why its called 'append'. 😉
              when you READ the lines you probably fill an array with the values from the textfile. maybe its easiest to reverse this array, before using the values for output.

              the last will be first then. 🙂

                aha! I wondered if that were possible, but how would you reverse it, is there a way to do that without adding a field to sort it on? (I assume there must be, since you said...I'm not asking for the code, just a hint as to where one might start - after all, I'm not writing anything that has any similiarity to this, just curious - wjat a run-on parenthetic statement that was, and without coffee! better go get some!)

                edit - still ain't went to get my first cup of coffee... but look what I found:

                http://www.phpbuilder.com/board/showthread.php?s=&threadid=10242205

                🙂

                  Well, to be honest, I don't know how that helped me, it was using the number of clicks..

                    If I were to use date, how would I do that?
                    If anyone could tell me how to arrange it by the date it was updated, that would be even better.
                    Also, I really have no clues on arrays, in the PHP book i got from library, I never really read about arrays..

                      If you want to have the newest message in the file first, then you will need to do something like this.

                      $intext = get_file_contents(newsa.txt);  // get the old stuff
                      $outtext = $message . $intext; // concatenate old to new
                      fopen("newsa.txt", "w"); // write out new and old
                      write($fp, $outtext);
                      fclose($fp);
                      

                        this reads a text-file "messages.txt" line by line and displays the contents
                        1. old messages first
                        2. latest messages first

                        <?
                        // defining the txt-file
                        $file = "messages.txt";
                        // open file for reading
                        $fp = fopen($file, "r");
                        // read contents to array
                        $i = 0;
                        while(!feof($fp)){
                                          // read each line up to 100 characters
                                          $line = fgets($fp, 100);
                                          /* make an array $content which contains the
                                          line 1 as $content[0]
                                          line 2 as $content[1]
                                          line 3 as $content[2] */
                                          $content[$i] = str_replace("\n", "<br>", $line);
                                          $i++;
                        }
                        
                        fclose($fp);
                        
                        
                        /* 1. OLD MESSAGES FIRST:
                        we have the array $content which contains the 
                        all lines of the txt-file, the oldest line first */
                        echo "old msgs first:<br />";
                        for($i=0; $i<sizeof($content);$i++){
                        echo $content[$i];
                        }
                        
                        
                        /* 2. LATEST MESSAGES FIRST:
                        as you want to echo out the last value first
                        you just have to reverse the array by using, eh...? guess. */
                        echo "new msgs first:<br />";
                        $backward = array_reverse($content);
                        for($i=0; $i<sizeof($backward);$i++){
                        echo $backward[$i];
                        }
                        ?>
                        
                          $outtext = $message . $intext; // concatenate old to new
                          

                          For that string of line, will it mess it up if I add the stuff like

                          topic = stripslashes($topic);
                          $message = stripslashes($message);
                          $posted = " Posted By: Balrok";
                          $curr_news = file("news.txt");
                          $fontshit = "<font color=\"#666666\" size\"=-2\">";
                          $message = "<b>" . $fontshit . $topic . $posted . "</b><br>\n" . $message . "<p>\n";
                          

                          ??

                            I cannot see why it would hurt. If I have

                            $oldstr = "<br>howdy neighbor!<br>";  //and
                            $newstr = "<br>should be the top line<br>";
                            // when they get concatenated you get
                            $catstr = $newstr . $oldstr;
                            echo "$catstr";
                            

                            You should see

                            should be the top line

                            howdy neighbor

                            IOW, if you can write the text in one chunk, there is no reason why you cannot write it in two and stick together.

                              Here is what I use for my news, feel free to use it or whatever...

                              $date_format = date("l F j, Y, h:i:s A");
                              
                              $endline = "\n\n";
                              
                              $news = "<p align='left'><i>News for:</i><b> " . $date_format . "</b></p>
                              
                                <p align='left'><b><img src='Bullet1.gif' width='15' height='13'>" . $title . "</b> <font size='2'>[posted by <a href='mailto:" . $email . "'>" . $user . "</a>]</font></p>
                                " . $news . "
                                </p>
                              
                                <p align='center'><i>[Visit our <a href='http://www.t4cbible.com/forum/'>forum</a> to post a comment]</i></p>
                                <p align='center'>]---[</p>".$endline;
                              
                              $filename = "news.txt";
                              $handle = fopen($filename, "r");
                              $old_content = fread($handle, filesize ($filename));
                              $handle = null;
                              $final_content = $news1.$old_content;
                              $handle2 = fopen($filename, "w");
                              $finalwrite = fwrite($handle2, $final_content);
                              $handle2 = null;
                              	}
                              
                              

                                hey foxhound, it seems that that will work.. but.. do you use MySQL for logging in and stuff ?

                                  No, actually I just use pure PHP. Never really gotten into SQL 🙂

                                    been following this thread because I wanted to see the finished product and try to implement it but when I tried, I had problems trying to write it out...here is what I have so far:

                                    <div class="center">
                                    <form action="http://www.atimealone.com/php/news/newstest.php" method="post" /> 
                                    <table style="text-align:center;border:0;"> 
                                    
                                    <tr><td class="g"><b>Name:</b></td>
                                    
                                    <td class="g"><input type="text" name="user" size="40"  value="Name"   onfocus="if(this.value=='Name')this.value='';" onblur="if(this.value=='')this.value='Name';"  /></td></tr> 
                                    
                                    <tr><td class="g"><b>Email:</b></td>
                                    
                                    <td class="g"><input type="text" name="email" size="40" value="email"  onfocus="if(this.value=='email')this.value='';" onblur="if(this.value=='')this.value='email';" /></td></tr> 
                                    
                                    <tr><td class="g" valign="top"><b>News:</b></td> 
                                    
                                    <td class="g"><textarea name="news1" cols="40" rows="2">News Test</textarea></td></tr>
                                    
                                    <tr><th colspan="2"><p><input type="submit" value="Add News" /> <input type="reset" value="Clear" /></p></th></tr> 
                                    </table>
                                    <?php 
                                      if ($_SERVER[REQUEST_METHOD]=="POST")
                                    
                                      {
                                    
                                     extract($_REQUEST);
                                    
                                    $date_format = date("l F j, Y, h:i:s A");
                                    
                                    $endline = "\n\n";
                                    
                                    $news = "<p class='g'><i>News for:</i> " . $date_format . "</p>
                                    
                                      <p class='h'><img src='http://www.atimealone.com/images/BlueDot.gif' width='10' height='10'>" . $title . "</b> [posted by <a href='mailto:" . $email . "'>" . $user . "</a>]</p>
                                      <p class='g'>
                                      " . $news . "
                                      </p>
                                    
                                      <p class='center'><----------------></p>".$endline;
                                    
                                    $filename = "news.txt";
                                    $handle = fopen($filename, "r");
                                    $old_content = fread($handle, filesize ($filename));
                                    $handle = null;
                                    $final_content = $news1.$old_content;
                                    $handle2 = fopen($filename, "w");
                                    $finalwrite = fwrite($handle2, $final_content);
                                    $handle2 = null;
                                       }
                                    echo $news;
                                    ?>
                                    </div>
                                    

                                    I'm pretty sure that the line echo $news; is wrong, how should I echo out the file once it's been written to?

                                      Do

                                      include('news.txt');

                                      That will include the whole file, not just the $news. 🙂

                                        Thanks! I should have thought of that, but I sure didn't!
                                        Anyway, so far so good, I'm not getting all the fields to write to the text file, but it's looking good:

                                        http://www.atimealone.com/php/news/newstest.php

                                        edit:
                                        aha, I had the wrong field in there a couple places, ok now, and here is the final script (including the input form):

                                        <form action="http://www.atimealone.com/php/news/newstest.php" method="post" /> 
                                        <table style="text-align:center;border:0;"> 
                                        
                                        <tr><td class="g"><b>Name:</b></td>
                                        
                                        <td class="g"><input type="text" name="user" size="40"  value="Name"   onfocus="if(this.value=='Name')this.value='';" onblur="if(this.value=='')this.value='Name';"  /></td></tr> 
                                        
                                        <tr><td class="g"><b>Email:</b></td>
                                        
                                        <td class="g"><input type="text" name="email" size="40" value="email"  onfocus="if(this.value=='email')this.value='';" onblur="if(this.value=='')this.value='email';" /></td></tr> 
                                        
                                        <tr><td class="g" valign="top"><b>News:</b></td> 
                                        
                                        <td class="g"><textarea name="news" cols="40" rows="2">News Test</textarea></td></tr>
                                        
                                        <tr><th colspan="2"><p><input type="submit" value="Add News" /> <input type="reset" value="Clear" /></p></th></tr> 
                                        </table>
                                        <?php 
                                          if ($_SERVER[REQUEST_METHOD]=="POST")
                                        
                                          {
                                        
                                         extract($_REQUEST);
                                        
                                        
                                        //stripslashes and replace url with link
                                        $news = stripslashes(ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]",
                                                             "<a href=\"\\0\">\\0</a>", $news));
                                        $date_format = date("l F j, Y, h:i:s A");
                                        
                                        $endline = "\n\n";
                                        
                                        $news1 = "<p class='f'><i>News for:</i> " . $date_format . " [posted by <a href='mailto:" . $email . "'>" . $user . "</a>]</p><p class='g'>" . $news . "</p><p class='center'><img src='http://www.atimealone.com/images/line.gif' width='379' height='6' alt='line.gif' /></p>".$endline;
                                        
                                        $filename = "news.txt";
                                        $handle = fopen($filename, "r");
                                        $old_content = fread($handle, filesize ($filename));
                                        $handle = null;
                                        $final_content = $news1.$old_content;
                                        $handle2 = fopen($filename, "w");
                                        $finalwrite = fwrite($handle2, $final_content);
                                        $handle2 = null;
                                           }
                                        include("news.txt");
                                        ?>
                                          Write a Reply...