Forum Moderators: coopster

Message Too Old, No Replies

reversed reading from a txt file

         

clown

9:26 pm on Apr 4, 2007 (gmt 0)

10+ Year Member



I'm a reall rookie when it comes to PHP scripting.. I have done some basic stuff, but now I've decided to start learning more advanced PHP...

So what my question is..I've made a shoutbox, but the last post is coming at the end of the shout list... So what I've figured out is that I might have to read the file from the bottom and up. I've been reading around on php.net about everything related to file() and fread()/fwrite() etc... but I cant get it to work...

So my question is basicly how?

right now my read code looks like this

<?php

$fh = @fopen("shout/msg.txt", "r");
if ($fh) {
$i = 1;
while (!feof($fh)) {
$shout[$i] = fgets($fh, 4096);
$i++;
}
while ($i = 1) {
echo $shout[$i];
$i-1;
}
fclose($fh);
}

?>

and i know it's wrong since it dont work.. Been trying to edit examples from php.net to fit what I need it to do, but with no luck..

fast4u

9:31 pm on Apr 4, 2007 (gmt 0)

10+ Year Member



Hey,

Try this

<?php

$filename = "filename.txt";
$fd = fopen($filename, "r");
$buffer = fread($fd, filesize($filename));
fclose($fd);
echo $buffer;
}
?>

clown

9:44 pm on Apr 4, 2007 (gmt 0)

10+ Year Member



didn't work... last post still comes at the end.

there's absolutely nothing there now except the shoutbox... since i started the site today... and I've decided to make everything myself =) so it's gonna take a lil time before i get it fully functional :)

[edited by: coopster at 9:55 pm (utc) on April 4, 2007]
[edit reason] no personal urls please TOS [webmasterworld.com] [/edit]

clown

9:58 pm on Apr 4, 2007 (gmt 0)

10+ Year Member



sorry admin... my bad... wont happen again

coopster

10:00 pm on Apr 4, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Forgiven ;)

You could use file() [php.net] to read the text file into an array and then use array_reverse [php.net].

clown

10:04 pm on Apr 4, 2007 (gmt 0)

10+ Year Member



thanks... i'll try that out... should i just use the:

<?php

$filename = "filename.txt";
$fd = fopen($filename, "r");
$buffer = fread($fd, filesize($filename));
fclose($fd);
echo $buffer;
}
?>

and the instead of using the echo i add it to the array and the use the reverse function? *going to read about arrays* never worked with it before... this is going to be interesting =)

clown

10:53 pm on Apr 4, 2007 (gmt 0)

10+ Year Member



well i've been going trough array functions, trying and failing and failing and failing and failing on my shoutbox =)

My code now is:
<?php
$fh = fopen("shout/msg.txt", "r");
if ($fh) {
while (!feof($fh)) {
$buffer = fgets($fh, 4096);
if ($buf_array == '') { $buf_array = $buffer; }
else { $buf_array = $buf_array . $buffer; }
}
fclose($fh);
$buffer = array($buf_array);
$shout = array_reverse($buffer);
print_r($shout);
}
?>

-------------
And it prints out:
Array ( [0] =>
Ole: Testing 1
Ole: Testin 2
)

So something is still not working...

clown

11:20 pm on Apr 4, 2007 (gmt 0)

10+ Year Member



admin... you can delete my last to posts here, cuz I've figured it out... only got one more question...

how do i strip the result for the Array info and only print out the info from the file... Cuz it looks a lil stupid with the Array() and [0] [1] etc... =)

cameraman

11:38 pm on Apr 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Instead of your print line:
print_r($shout);

do this:
foreach($shout as $oneyell)
echo $oneyell . '<br />';

and the array stuff won't be displayed.

clown

11:49 pm on Apr 4, 2007 (gmt 0)

10+ Year Member



thank you so much guys... it worked! you have a good community going here... people who knows what they're talking about and also quick replies... thanks for helping a step further on my site =) *not gonna post the url again* hehe

joelgreen

10:05 am on Apr 5, 2007 (gmt 0)

10+ Year Member



You could also try this.

// this will read file into array, each line into separate array item
$text = file('filename.txt');

// loop through array and output contents
for($i = sizeof($text); $i > 0; $i--) {
echo $text[$i-1] . "<br />";
}

OR this

// this will read file into array, each line into separate array item
$text = file('filename.txt');

// reverse array
$text= array_reverse($text);

// loop through array and output contents
foreach($text as $line) {
echo $line . "<br />";
}

First one is a little faster since it will not have to reverse array, while second is easier to understand