Page 1 of 1

displaying the whole file

Posted: Tue Apr 29, 2003 7:03 pm
by Atomic Monkey
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?

Posted: Tue Apr 29, 2003 7:43 pm
by Sevengraff
it's only displaying the first, because file() makes an array with each element being a line, and your not advancing the array.

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

?>
I belive this will work how you want it too.