PDA

View Full Version : reading certain number of lines php



kabatak
September 11th, 2002, 11:36
If the data.txt file that I want to read has 20 lines, what will I do if I only want to read/display the last 10 lines?
And also, what will I do if I would like to reverse the order of lines when I read them?

Salam
September 12th, 2002, 13:14
For displaying line 10 to the end of file :

$f = file ("data.txt") ;
for ($i=9; isset($f[$i]); $i++) echo $f[$i] ;

For displaying in reverse order :

$f = file ("data.txt") ;
for ($i=19; $i>=0; $i--) echo $f[$i] ;

kabatak
September 12th, 2002, 13:25
Thank you Salam.
I have a question, in reading in reverse order do i have to specify the number of lines that I have like in what you wrote?


$f = file ("data.txt") ;
for ($i=19; $i>=0; $i--) echo $f[$i] ;

You specified $i=19 but what if I do not know how many lines my data.txt has, say it is a guestbook and I don't know exactly how many lines it already has but all I want is to read them in reverse. How will I do that?

Salam
September 12th, 2002, 13:32
$f = file ("data.txt") ;
for ($i=0; isset($f[$i]); $i++)
for ($j=$i-1; $j>=0; $j--) echo $f[$j] ;

kabatak
September 12th, 2002, 13:43
Thank you very much Salam, I really appreciate your help :)