Read a text file to a text area backwards
Moderator: General Moderators
Read a text file to a text area backwards
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");
?>
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");
?>
there is no auto-insert-at-top-of-file.
You can append text to a file (at the end) with something likeor you can read the whole file, insert a new line to the array at the beginning and write it back to the file
You can append text to a file (at the end) with something like
Code: Select all
fputs(fopen($file, 'a'), $text);Code: Select all
$contents = file($filename);
array_unshift($contents, $newline);
$fd = fopen($filename, 'w');
foreach($contents as $line)
{
echo $line;
fputs($fd, $line);
}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.
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.
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/
I'm certain you can handle it
http://www.php.net/manual/en/
cut n paste
No I can use cut and paste but what shall I cut and where do I paste it
????
Yes this is very urgent.....
????
Yes this is very urgent.....
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>- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Not sure what double line break you're talking about, do you mean you need to change this:
to this
Mac
Code: Select all
$contents = $_POSTї'min']."\n".$contents;Code: Select all
$contents = $_POSTї'min']."\n\n".$contents;Mac
Thank you all
Ok now the line break works as well cool .......
I thank you bye bye
Cheers
I thank you bye bye
Cheers