Read a text file to a text area backwards

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
iceb
Forum Newbie
Posts: 17
Joined: Mon Oct 28, 2002 6:18 am

Read a text file to a text area backwards

Post 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");
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

there is no auto-insert-at-top-of-file.
You can append text to a file (at the end) with something like

Code: Select all

fputs(fopen($file, 'a'), $text);
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);
}
DeGauss
Forum Contributor
Posts: 105
Joined: Tue Oct 22, 2002 9:44 am
Location: Gainesville, FL

Post 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.
iceb
Forum Newbie
Posts: 17
Joined: Mon Oct 28, 2002 6:18 am

ok

Post 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
DeGauss
Forum Contributor
Posts: 105
Joined: Tue Oct 22, 2002 9:44 am
Location: Gainesville, FL

Post by DeGauss »

Are you incapable of using Cut & Paste?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

is it that urgent? if not then this will be a good exercise ;)
I'm certain you can handle it 8)

http://www.php.net/manual/en/
iceb
Forum Newbie
Posts: 17
Joined: Mon Oct 28, 2002 6:18 am

cut n paste

Post 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.....
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

ok, then try this (completely untested, not even by compiler)

Code: Select all

&lt;?PHP
	$filename ="todo.txt"; 
	$handle = fopen($filename, 'r');
	$contents = '';
	while($part = fread($handle, 2048))
		$contents .= $part;
	fclose($handle);
	if (isset($_POST&#1111;'submit']) and $_POST&#1111;'submit']) 
	{ 
		if (isset($_POST&#1111;'min']) &amp;&amp; strlen($_POST&#1111;'min']) &gt; 0) 
		{
			$contents = $_POST&#1111;'min']."\n".$contents;
			$handle = fopen($filename, 'w');
			fputs($handle, $contents);
			fclose($handle);
		} 
	} 
?&gt;&lt;html&gt;&lt;head&gt;&lt;/head&gt; 
&lt;body&gt; 
	&lt;br&gt; 
		&lt;form ACTION="reader.php" METHOD="POST"&gt; 
			&lt;textarea readonly rows="20" cols="40"&gt;&lt;?php echo $contents; ?&gt;&lt;/textarea&gt;&lt;br/&gt; 
			&lt;input type="text" TABINDEX=1 size="56" name="min" /&gt; 
			&lt;button TABINDEX=2 type=submit name=submit value="Send"&gt;Send&lt;/button&gt; 
		&lt;/form&gt; 
&lt;/body&gt; 
&lt;/html&gt;
iceb
Forum Newbie
Posts: 17
Joined: Mon Oct 28, 2002 6:18 am

ok error

Post 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 ?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Change && to &&.

Mac
iceb
Forum Newbie
Posts: 17
Joined: Mon Oct 28, 2002 6:18 am

wow

Post by iceb »

ok cool

now it works but where is my

double line break ?

Thanks
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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&#1111;'min']."\n".$contents;
to this

Code: Select all

$contents = $_POST&#1111;'min']."\n\n".$contents;
:?:

Mac
iceb
Forum Newbie
Posts: 17
Joined: Mon Oct 28, 2002 6:18 am

Thank you all

Post by iceb »

Ok now the line break works as well cool .......

I thank you bye bye

Cheers :D
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

twigletmac wrote:Change && to &&.

Mac
I would be using AND if I felt not like a VB-scripter then ;)
Post Reply