Page 1 of 1

Reversing the order

Posted: Sun Jun 15, 2003 12:57 am
by AbrasiveRock
Ok, I hope this has not been posted already. I tried to do a search, but the search seems to be broken. Anyways, the following is my entire code with nothing taken out...

Code: Select all

<?php 
if ($_POST&#1111;'submit']=="submit") &#123; 
   $savestring = date("d/m/Y") . "<br>" . $_POST&#1111;'newsletter'] .  "<P>"; 
   $fp = fopen("newsletter.txt","a"); 
   fwrite($fp,$savestring); 
   fclose($fp); 

header("Location: http://www.carrasdesign.com/oly/newsletter.php"); 
&#125; 
?> 
<html><body> 
<form action="<?php echo $_SERVER&#1111;'PHP_SELF'] ?>" method=post > 
<pre> 
You are about to add content to the UN-OFICIAL Olympia Custodians, Transportation, & Maintenance newsletter:<br><textarea rows=23 cols=92 name='newsletter'></textarea> 
<input type=submit value=submit name="submit"> 
</form> 

</body> 
</html>
I am trying to make a comments page. It works great except it's in the reverse order I want it to be in. Any simple way to do this?

Posted: Sun Jun 15, 2003 2:51 am
by patrikG
Use array_reverse when you read or write to the textfile.

Posted: Sun Jun 15, 2003 3:03 am
by AbrasiveRock
patrikG wrote:Use array_reverse when you read or write to the textfile.
So instead of or in addition to the fwrite? I had a feeling that the array_reverse was the tool, but I had no clue as to how it fit into the whole thing. Every example of the array_reverse I have in my PHP books (I only have two) makes no sense with what I have learned so far.

Posted: Sun Jun 15, 2003 3:38 am
by patrikG
Try

Code: Select all

<?php
$myFileContent=array_reverse(file("newsletter.txt"));
foreach ($myFileContent as $key=>$value)
        echo "<br>line: $key - $value";
?>
file returns an array which is immidiatly reversed through array_reverse.

Posted: Sun Jun 15, 2003 11:17 pm
by AbrasiveRock
Ok, I think I'm even more confused. Is this something I put before what I already have or after? I notice that your script does not have the date or the header location part, so I'm going to assume that I shouldn't replace what I had with this...right? Sorry for so many questions.

Posted: Mon Jun 16, 2003 1:10 am
by corlando
basically what you need to is preappend the new entry before every thing else in your file. they only way to do that is below

Code: Select all

<?php
$newsletter_filename = "newsletter.txt";
$temp_filename = "tempfile.txt";

// make sure the newsletter field has been filled
if ( isset( $_POST['newsletter']) == "submit" && $_POST['newsletter'] != "" ) { 

	$savestring = date("d/m/Y") . "<br>" . $_POST['newsletter'] .  "<P>"; 

	// create a temp file for writing to
	$file_temp = fopen( $temp_filename, "w");
	
	// open original file for reading from
	$file_orig = fopen( $newsletter_filename, "r"); 
		
	// put new entry into temp file
	fputs( $file_temp, $savestring );

	/*	
	This while loop	loops through each line
	in the original file and puts it in the temp file	 
	*/
	while (! feof( $file_orig ) ) {				
		fputs( $file_temp, fgets($file_orig) );	
	}	
	
	// close files
	fclose($file_temp); 
	fclose($file_orig);
	
	
	unlink($newsletter_filename); // delete original file
	rename($temp_filename, $newsletter_filename);	//rename tempfile to original file

	header("Location: http://www.carrasdesign.com/oly/newsletter.php"); 

}

?>
OR the array_reverse method (must make sure that there is only one entry per line

Code: Select all

16/06/2003&lt;br&gt;helol&lt;br&gt;&lt;P&gt;
16/06/2003&lt;br&gt;hwlloe world&lt;P&gt;
16/06/2003&lt;br&gt;dfsg&lt;P&gt;
16/06/2003&lt;br&gt;cxvz&lt;P&gt;
16/06/2003&lt;br&gt;sdfsd&lt;P&gt;

Code: Select all

<?php

$newsletter_filename = "newsletter.txt";

// make sure the newsletter field has been filled
if ( isset( $_POST['newsletter']) == "submit" && $_POST['newsletter'] != "" ) { 

	$savestring = date("d/m/Y") . "<br>" . $_POST['newsletter'] .  "<P>"; 

	// open original file for appending
	$file_orig = fopen( $newsletter_filename, "a"); 
		
	// put new entry into file on one line only converting newline characters to <Br>
	fputs( $file_orig, str_replace("\n", "<br>", $savestring ) . chr(13) . chr(10) );

	// close file
	fclose($file_orig);

	header("Location: http://www.carrasdesign.com/oly/newsletter.php"); 
}


// This is how you reverse the contents of the file and display it on screen
echo implode("", array_reverse( file($newsletter_filename) ) );

?>

Posted: Mon Jun 16, 2003 3:22 am
by patrikG
AbrasiveRock wrote:Is this something I put before what I already have or after? I notice that your script does not have the date or the header location part, so I'm going to assume that I shouldn't replace what I had with this...right?
I should have explained a bit better - if you are familiarised with the file-function it gets easier.

the problem: You want a textfile to be displayed and stored (in variables) in reverse order.

a solution:
1) read the textfile as an array, not a string
2) reverse the array

This is exactly what this line does:

$myFileContent=array_reverse(file("newsletter.txt"));

as file returns an array (each new line in the textfile is one item in the array).

The point you will have to decide is:

where do you need the array reversed - before writing to the textfile or after writing to the textfile - or for display?

Pick one of the methods from corlando's post :)