Ok, so I've set up my system to write to a .txt file then display it on the main page. However, it only displays the last entry and does not display the entire file. Here's the code:
post.html
<form action="index.php" method="POST">
Input News: <input type="text" name="news" />
Input your forum name: <input type="text" name="posted" />
<input type="submit">
</form>
index.php
<?
if($HTTP_SERVER_VARS['REQUEST_METHOD'] == "POST"){
$newsinfo[0] = $_POST['news'];
$newsinfo[1] = $_POST['posted'];
$file = fopen("news.txt",'a');
fwrite($file,$_POST['news'] . "|" . $_POST['posted'] . "\n");
fclose($file);
} else {
$farray = file("news.txt");
$newsinfo = split("\|",$farray[(sizeof($farray)-1)]);
}
echo "News: " . $newsinfo[0] . "<P>Posted by: " . $newsinfo[1];
?>
Obviously the two txt files are new.txt and posted.txt. I suppose my question is how do I solve the problem of not posting the entire .txt file?
displaying the whole file
Moderator: General Moderators
-
Atomic Monkey
- Forum Newbie
- Posts: 7
- Joined: Sun Apr 20, 2003 11:19 am
- Location: Arkansas
- Contact:
- Sevengraff
- Forum Contributor
- Posts: 232
- Joined: Thu Apr 25, 2002 9:34 pm
- Location: California USA
- Contact:
it's only displaying the first, because file() makes an array with each element being a line, and your not advancing the array.
I belive this will work how you want it too.
Code: Select all
<?
if($HTTP_SERVER_VARS['REQUEST_METHOD'] == "POST"){
$newsinfo[0] = $_POST['news'];
$newsinfo[1] = $_POST['posted'];
$file = fopen("news.txt",'a');
fwrite($file,$_POST['news'] . "|" . $_POST['posted'] . "\n");
fclose($file);
echo "News: " . $newsinfo[0] . "<P>Posted by: " . $newsinfo[1];
} else {
$farray = file("news.txt");
$size = count($farray) // get num of elements
for($i=0; $i<$size; $i++) { // go through all elements of $farray
$newsinfo = explode("|", $farray[$i]); // split lines of file
echo "News: " . $newsinfo[0] . "<P>Posted by: " . $newsinfo[1]; // output
} // end for
} // end if
?>