TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Make a txt file display in reverse (http://www.talkphp.com/advanced-php-programming/5061-make-txt-file-display-reverse.html)

gallienne09 10-26-2009 10:43 PM

Make a txt file display in reverse
 
Can some one tell me how to reverse the text file so it displays what goes in last first.

Cheers in advance

here is the code:

<html>
<head>
<title>test Text</title>
</head>
<body>
<div style='overflow:auto; width:230px; height:500px; background-color:orange'>
<fieldset width="200">
<legend>
<b>Message Board:</b>
</legend>

<form method="POST" action="text.php" style="font-family:verdana; font-size:10pt">
Nickname:<br><input type="text" name="name" size="25"/><br>
Comment:<br><textarea name="comment" rows="4" cols="23">
</textarea><br>
<input type="submit" value="Submit" />
<input type="reset" value="Reset" />
</form>
</fieldset>

<?php
$date= date("d/m/y");
$name=$_POST['name'];
$comment=$_POST['comment'];

if ($comment != "" )
{
$myFile = "testFile.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "<b>Posted: $date</b>\n<br>";
fwrite($fh, "$stringData");
$stringData = "\"$comment\"\n<br>";
fwrite($fh, "$stringData");
$stringData = "<b>By:$name</b>\n<hr>";
fwrite($fh, $stringData);
fclose($fh);
}

else
{
echo "<b style='color:red'>Please fill in all fields!</b>";
}
?>


<?php

$myFile = "testFile.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);

echo "
<table border='0' width='210' align='center'>
<tr>
<td style='font-family:verdana; font-size:8pt'>
$theData
</td>
</tr>
</table>


";
?>
<?php


?>
</div>
</body>
</html>

delayedinsanity 10-27-2009 01:58 AM

php Code:
$arr = file( YOUR_FILE );
$arr = array_reverse( $arr );

gallienne09 10-27-2009 02:17 AM

Thanks for the code. could you elaborate on this code as to where to use it in my code above. thanks in advance

delayedinsanity 10-27-2009 03:22 AM

That takes all the fun out of learning. :)

php Code:
$myFile = "testFile.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);

Would be...

php Code:
$myFile = 'testFile.txt';
$theData = array_reverse( file( $myFile ) );

foreach ( $theData as $row )
    echo $row . '<br />';

Or however you wish. There's about 3,141.59265 ways to do anything in PHP.

gallienne09 10-27-2009 06:14 AM

Thank You very much. Works a treat, didnt relise how easy that was gonna be.

cheers

caiyanfang 03-30-2013 07:56 AM

'Lindsay's been telling everyone that she moved to Ny because she was sick of ralph lauren outlet online la. But that's simply not true, she so want to move back gulf of mexico, but she won't.

'In the past, Lindsay has were able to rope in anyone to co-sign out on leases because her credit score means she aren't going to get approved in her name alone. ralph lauren polo outlet shop

'However, she couldn't verify anyone polo ralph lauren outlet online willing accomplish this now as brand-new areas such as burned so any one of her friends and are associates. '

programmingservices 05-22-2013 11:58 AM

I think this is the best tutorial info regarding programmer; I really like to read it; and I hope it’s very helpful and useful to me, Thanks sharing for programmer; great going.

sunflower 05-23-2013 06:11 AM

Your content are perfect and get to themes or templates, that are unable to always be recognized all over the place. Please continue penning this sort of fantastic materials, it could be absolutely important.
Curtis Granderson Jersey
Customized New York Yankees Jersey
Don Mattingly Jersey


All times are GMT. The time now is 02:07 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0