How do I display the last entry of a .txt file

They have: 16 posts

Joined: Apr 2002

How do I display the last entry of a .txt file in a textarea..?

Everytime I try I get the 'beginning of file' not 'end of file' displaying which means you have to scroll down to read the last entry.

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

which method are you using to read the file?

try

<?php
    $data
= file('filename.txt');
   
   
// $data is an array with each line of filename.txt
    // as a separate element. Now just show last one
   
   
echo $data[count($data)];
   
?>

-Greg

They have: 16 posts

Joined: Apr 2002

Thanx Greg - I am hoping to show all the txt file or at least the last few lines - I am using 'echo' to read to screen at the moment but have tried the 'include' which made no difference, still started showing at the top which meant I had to scroll down to the bottom..

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

another example:

<?php
    $data
= file('filename.txt');
   
   
// Set a variable to the size of the array. This
    // is more efficient and faster than calling the
    // function over and over in the loop below.
   
$maxlines = count($data);
   
   
// This is how many lines to show
    // To show them all, do $lines2show = $maxlines
   
$lines2show = 5;
   
   
// This will loop through until the number you specified
    // (or until all data is display; ie. you say show 5,
    //                          but there is only 3 items)
   
for($t=0; ($t<$lines2show && $t<$maxlines); $t++)
    {
        echo
$data[$maxlines-$t];
    }
   
?>

They have: 16 posts

Joined: Apr 2002

I run your piece above and got this -

Parse error: syntax error, unexpected T_VARIABLE

<?php
       $data
= file('comments.txt');        
      
$maxlines = count($data);         
       do
$lines2show = $maxlines    
       $lines2show
= 5;   //--------------------------- ths is the offending line   
      
for($t=0; ($t<$lines2show && $t<$maxlines); $t++)
       {
       echo
$data[$maxlines-$t];
       }
?>

what would cause this error?

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

Right above the offending line you have the last half of a comment line that I had.

<?php
do $lines2show = $maxlines
?>

Remove that line.

-Greg

They have: 16 posts

Joined: Apr 2002

It would appear I didn't submit the apology here, or i did it wrong..
I'm having a bad hairday I'm afraid..

As mentioned earlier in the HTML forum both your above suggestions failed to work..
Perhaps if I show you what I got so far you might be able to advise something..

<?php
if(array_key_exists('name', $_POST)){
$fh = fopen('comments.txt', 'a+');
$fcontent = fread($fh, filesize("comments.txt"));
$towrite = "\n".$name;
fwrite($fh, $towrite);
fclose($fh);
}
?>

Comments

<?php
    
echo $fcontent . $towrite
?>

Add your 'Name & comment' here then click 'Enter'

I thought the 'a' in the fopen would have put the pointer to the bottom of the file but obviously not for what I want..
This works fine except for the displaying, I still have to scroll down to read the latest entries..

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

Well here is a working example, using the method I gave that will display a set # of lines:

http://www.wtvgames.com/filetest/displines.php
The code for it can be seen here: http://www.wtvgames.com/filetest/displines.txt
The contents of the data file is: http://www.wtvgames.com/filetest/comments.txt

I even took the example, and gae two options, it will either display the last XX lines of the comments file, either with the oldest first, or newest first. Just use the section of code for whichever style you wanted.

-Greg

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.