Page 1 of 1
Read a text file to a text area backwards
Posted: Mon Oct 28, 2002 6:18 am
by iceb
Hi
Does anyone know how to read a text file and then display it
backwards in a text area ? Starting with the LAST and NEWEST
line and ending with the FIRST and eldest line ?????
Here is what I have got so far ......
The current problem is that it only write one line and then it
is overwritten when the next line is send. I want it to write a line
and then add it to the text file and then read the whole text
file like I mentioned above.........
LINK for reader.php
http://212.242.56.117/reader.php
CODE for reader.php
<?PHP
if (isSet($_POST['submit']) and $_POST['submit'])
{
if ($_POST['min'])
{
$min = $_POST['min'] . "\n\n";
// skriv linie i fil
$filename ="todo.txt";
$handle= fopen($filename,'r ');
fputs($handle, $min);
fclose($handle);
}
}
?>
<html>
<head>
</head>
<body>
<br>
<form ACTION="reader.php" METHOD="POST">
<textarea readonly rows="20" cols="40"><?php readFile("todo.txt");?></textarea><br>
<input type="text" TABINDEX=1 size="56" name="min">
<button TABINDEX=2 type=submit name=submit value="Send">Send</button>
</form>
</body>
</html>
<?PHP
show_source("reader.php");
?>
Posted: Mon Oct 28, 2002 6:44 am
by volka
there is no auto-insert-at-top-of-file.
You can append text to a file (at the end) with something like
or you can read the whole file, insert a new line to the array at the beginning and write it back to the file
Code: Select all
$contents = file($filename);
array_unshift($contents, $newline);
$fd = fopen($filename, 'w');
foreach($contents as $line)
{
echo $line;
fputs($fd, $line);
}
Posted: Mon Oct 28, 2002 6:45 am
by DeGauss
use fopen to open the file, then insert each line into an array.
Then use a for loop, but instead of incrementing your control variable, decerement it.
$file=fopen("file");
(i can't remember the code to put each line of a file into an array, but let's pretend the following lines will work)
$fileArray=array();
$fileArray[]=NextLineOfFile;
(okay, back to working code)
print "<textarea>";
for ($x=count($fileArray);$x>=0;$x--) {
print $fileArray[$x];
}
print "</textarea>";
... Or something like that.
ok
Posted: Mon Oct 28, 2002 6:53 am
by iceb
Hi
could you please take my code and then put
your code into it ?
I am new to php so please help me out.....
Thanks
Posted: Mon Oct 28, 2002 6:58 am
by DeGauss
Are you incapable of using Cut & Paste?
Posted: Mon Oct 28, 2002 7:00 am
by volka
is it that urgent? if not then this will be a good exercise

I'm certain you can handle it
http://www.php.net/manual/en/
cut n paste
Posted: Mon Oct 28, 2002 7:02 am
by iceb
No I can use cut and paste but what shall I cut and where do I paste it
????
Yes this is very urgent.....
Posted: Mon Oct 28, 2002 7:25 am
by volka
ok, then try this (completely untested, not even by compiler)
Code: Select all
<?PHP
$filename ="todo.txt";
$handle = fopen($filename, 'r');
$contents = '';
while($part = fread($handle, 2048))
$contents .= $part;
fclose($handle);
if (isset($_POSTї'submit']) and $_POSTї'submit'])
{
if (isset($_POSTї'min']) && strlen($_POSTї'min']) > 0)
{
$contents = $_POSTї'min']."\n".$contents;
$handle = fopen($filename, 'w');
fputs($handle, $contents);
fclose($handle);
}
}
?><html><head></head>
<body>
<br>
<form ACTION="reader.php" METHOD="POST">
<textarea readonly rows="20" cols="40"><?php echo $contents; ?></textarea><br/>
<input type="text" TABINDEX=1 size="56" name="min" />
<button TABINDEX=2 type=submit name=submit value="Send">Send</button>
</form>
</body>
</html>
ok error
Posted: Mon Oct 28, 2002 7:32 am
by iceb
Hi
There is an error in line 10
Parse error: parse error, unexpected ';' in C:\Inetpub\wwwroot\reader.php on line 10
This is line 10
if (isset($_POST['min']) && strlen($_POST['min']) > 0)
how come ?
Posted: Mon Oct 28, 2002 7:33 am
by twigletmac
Change && to &&.
Mac
wow
Posted: Mon Oct 28, 2002 7:38 am
by iceb
ok cool
now it works but where is my
double line break ?
Thanks
Posted: Mon Oct 28, 2002 7:41 am
by twigletmac
Not sure what double line break you're talking about, do you mean you need to change this:
Code: Select all
$contents = $_POSTї'min']."\n".$contents;
to this
Code: Select all
$contents = $_POSTї'min']."\n\n".$contents;
Mac
Thank you all
Posted: Mon Oct 28, 2002 7:47 am
by iceb
Ok now the line break works as well cool .......
I thank you bye bye
Cheers

Posted: Mon Oct 28, 2002 7:51 am
by volka
twigletmac wrote:Change && to &&.
Mac
I would be using AND if I felt not like a VB-scripter then
