Need help writing information...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Drachlen
Forum Contributor
Posts: 153
Joined: Fri Apr 25, 2003 1:16 am

Need help writing information...

Post by Drachlen »

Hey guys... I need help again. I have a page which i use to post news, after i submit it goes to a file that writes the information to a document which is included on the main page. The problem is, when i post new news, its added below the older news. Is there any way to tell it to write it at the top instead of the bottom?

This is what i use to write the information:

Code: Select all

<META HTTP-EQUIV="refresh" content="2;URL=Http://morte-satanica.cjb.net">
<?PHP
$name = $_GET['name']; 
$news = $_GET['news'];

$da = date("F j, Y h:i:s A T");

$str = "<br><TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 width=590 bgcolor=#404040>
	<TD width=550 align=left bgcolor=#363636><img src=lcor.png><font face=Verdana Color=#7F8F7F><b>Posted by: $name</TD>
	<TD width=550 align=right bgcolor=#363636><font face=Verdana Color=#7F8F7F valign=top><b>$da<img src=rcor.png></TD>
	<TR>
	<TD colspan=2 bgcolor=#404040><font face=Verdana Color=#C9C8C1>&nbsp;&nbsp;&nbsp;&nbsp;$news</TD>
	</table>";

$fp = fopen("./newslog11.html", "a+"); 


fputs($fp, $str); 


fclose($fp);

   echo "<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0>
	 <TD>The news has been updated. You will be forwarded there now. If your browser doesn't send you, click <a href=http://morte-satanica.cjb.net/>here</a></table>";
?>
-Drachlen
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

mode

Post by phpScott »

There are several modes that you can use to add info to a file.

You are currently using a+ which will append new lines to the end of the file.

There is also r+ which says :OPEN a file for reading and writinf, Data will be written to the beginning of an existing file.

so your line

Code: Select all

$fp = fopen("./newslog11.html", "a+");
should read

Code: Select all

$fp = fopen("./newslog11.html", "r+");
phpScott
Drachlen
Forum Contributor
Posts: 153
Joined: Fri Apr 25, 2003 1:16 am

Post by Drachlen »

Thank you... It works but it seems to delete the old entry.. Is the any way i can keep the older stuff?
Drachlen
Forum Contributor
Posts: 153
Joined: Fri Apr 25, 2003 1:16 am

Post by Drachlen »

Anyone?
r77
Forum Newbie
Posts: 1
Joined: Fri Jun 20, 2003 2:57 pm

Post by r77 »

I would also be interested to fiogure this out :)
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

two files

Post by phpScott »

You could write the existing file to a temp file then write the new stuff into the existing file the write the stuff from the temp file back in.
Might not be effiecient but it would work.

Do you not access to a db to store the stories in because you could then just time stamp the each record and then order them by date/time.

phpScott
Post Reply