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!
i wrote a news script it has a form that i enter the new news into, then my php script parses the form and writes it to a text file, it writes it to the bottom of the file, i'd like it so it writes it to the top so the newest news is posted at the top of the file.
<?php
if((!isset($_POST['subject'])) or (!isset($_POST['news'])))
{
echo 'You must enter <b>ALL</b> fields before you can continue!';
exit;
}
$subject = $_POST['subject'];
$news = $_POST['news'];
$time = date("D d of F, h:i a");
$oldnews = file("news.txt");
$fp = fopen("news.txt", "w+");
$insertcode = "<table width='100%' border='0' cellspacing='1' cellpadding='2' bgcolor='#dcdcdc' align='center'><tr><td width='100%' border='1' bgcolor='whitesmoke'>".$subject." - ".$time."</td></tr><tr><td width='100%' border='1' bgcolor='#ffffff'>".$news."</td></tr></table><br>$oldnews";
fwrite($fp, $insertcode);
fclose($fp);
?>
see what I did.... I added $oldnews = file("news.txt"); to get the old news from the textfile. Then opened the file with w+ which empties the file out and makes it writable. It's ok for it to empty it out cause we already have the data in the $oldnews variable. Then just stick the $oldnews on the end of your new news..... voila
btw lilpunk... he was right to use w+ for emptying the file out.
php.net wrote:'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
php.net wrote:'r+' Open for reading and writing; place the file pointer at the beginning of the file.
r+ would do the same thing your code does, but without having to read the file into a variable first.. it just adds the new stuff to the top, like he wanted
honnestly this still doen not do what i want it too, i want it to insert the new code into the top of the txt file so the newest stuff is displayed at the yop of the file, r+ just inserts it to the top but it overwrites whatever was underneath
Im making a news poster to. And i have problems. Im trying to do it in Arrays now. So they can deleate 1 post at a time and not all of them and so they cant see teh posts unless they go to the page. IM me on AIM: The Hidden Viper. And ill try tp help as soon as i can get this done or MSN: wdd82689@mchsi.com or yahoo: the_hidden_viper or ICQ: 232372049. Thats about all of the instant messengers i have dont have IRC sorry. There all on so. Im me when ready cya
mode Description
'r' Open for reading only; place the file pointer at the beginning of the file.
'r+' Open for reading and writing; place the file pointer at the beginning of the file.
'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
'a' Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
'a+' Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
'x' Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call. This option is supported in PHP 4.3.2 and later, and only works for local files.
'x+' Create and open for reading and writing; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call. This option is supported in PHP 4.3.2 and later, and only works for local files.
<?php
if((!isset($_POST['subject'])) or (!isset($_POST['news'])))
{
echo 'You must enter <b>ALL</b> fields before you can continue!';
exit;
}
$subject = $_POST['subject'];
$news = $_POST['news'];
$time = date("D d of F, h:i a");
$oldnews = file("news.txt");
$fp = fopen("news.txt", "w");
$insertcode = "<table width='100%' border='0' cellspacing='1' cellpadding='2' bgcolor='#dcdcdc' align='center'><tr><td width='100%' border='1' bgcolor='whitesmoke'>".$subject." - ".$time."</td></tr><tr><td width='100%' border='1' bgcolor='#ffffff'>".$news."</td></tr></table><br>$oldnews";
fwrite($fp, $insertcode);
fwrite($fp, join('',$oldnews);
fclose($fp);
?>
The only real difference between the above code and lc's code is that there is no point in using w+ as you have no call to open it for reading, just writing... Also added the line to write the old data back to the file after the new line.