Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to append to the top of of a file?
#1
I was looking over the blog tool I made. And I noticed that It formatted like this:

(1/1/09) Post #1
This is the first post

(1/2/09) Post #2
This is the second post

(1/3/09) Post #3
This is the third post

I would like the 3rd post to be on top of the first and second, how can I do this?
Reply
#2
You can do that by first reading the contents of the file and then adding the new post at the begin of the file content, in the end save it.
I wouldn't do that, I would more suggest you to read the file into an array, line by line or preg_ functions.

After you have the array you look here for a sorting function that suits your needs.
I'll post a script in few mins, I'm busy right now!

EDIT:
When you save the posts, you can use bbCodes,
Code:
[poster]Motu[/poster][date]24.12.2009 01:39 AM[/date][post]Something[/post]

You would save this as single line into your file, then you can use the preg_replace function to sort the data.
Take a look here for the preg_replace
http://www.supportforums.net/showthread.php?tid=1676
Reply
#3
Well, if you don't use SQL, I would do something like this :

PHP Code:
<?php

// We retrieve all the data inside file.txt
$file_content file_get_contents("file.txt");

// We write our news
$news "blah this is the news !";

// We concatenate our new news, then our old file content
$new_file_content $news.$file_content;

// We write the new one !
file_put_contents("file.txt"$new_file_content);

?>

So, each time you will write into your file, the last news will always be on the top of your file.



Edit :
Oh, but, i read again your question, and i'm not sure of what you want ; Do you want to grap and write *only* the 3rd news on the top, or write all the last nextnews on the top of the file ?
Reply
#4
Good script Spl3en... I think you did what he wanted to know!
But like I said I would sort that upon loading...

PHP Code:
<?php

$handle 
fopen("test.txt""r");

$contents = array();

while(!
feof($handle)) {
    
$contents[] = fgets($handle4096);
}

$ccount count($contents) - 1;

for(
$i $ccount$i >= 0$i--) {
    echo 
$contents[$i]."<br />";
}

//edit
fclose($handle);

?>

The contents of the file look like this:

Code:
[poster]Motu[/poster][date]24.12.2009 01:39 AM[/date][post]Something[/post]
[poster]Motu[/poster][date]24.12.2009 01:38 AM[/date][post]Something[/post]
[poster]Motu[/poster][date]24.12.2009 01:37 AM[/date][post]Something[/post]
[poster]Motu[/poster][date]24.12.2009 01:36 AM[/date][post]Something[/post]

And the output would be.
Code:
[poster]Motu[/poster][date]24.12.2009 01:36 AM[/date][post]Something[/post]
[poster]Motu[/poster][date]24.12.2009 01:37 AM[/date][post]Something[/post]
[poster]Motu[/poster][date]24.12.2009 01:38 AM[/date][post]Something[/post]
[poster]Motu[/poster][date]24.12.2009 01:39 AM[/date][post]Something[/post]
Reply
#5
Quote:
PHP Code:
while(!feof($handle)) {
    
$contents[] = fgets($handle4096);

You really should use file function, it returns the file into an array like you, but has some other parameters, and a FALSE return value if there is an error Smile
It does automatically the fopen, fclose, so, you can't mistake with that Smile

Example :
PHP Code:
<?php

$array_file 
file("text.txt");

print_r($array_file);
var_dump($array_file);

?>

Is going to output :
Quote:// Print_r
Array
(
[0] => Hi

[1] => this is a test!

[2] => My name is text.txt !

[3] => This is the 4th line !

[4] => And this is the last one Smile
)


// Var dump
array(5) {
[0]=>
string(3) "Hi"
[1]=>
string(16) "this is a test!"
[2]=>
string(22) "My name is text.txt !"
[3]=>
string(23) "This is the 4th line !"
[4]=>
string(27) "And this is the last one Smile"
}


I find it useful Smile
Reply
#6
Yeah you're right, I was in a hurry....
And as you can see I forgot the fclose() Tongue
Reply
#7
Ah, i hadn't noticed too Big Grin
Well , i think that your manner is really better, i mean, organizing data into an array appear to be really more "logic" Smile
My solution isn't really proper, you can't analyse each line, it just does copy / paste and that's all xD

So nevets04, you should take the array solution if you would like to do something flexible Smile
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)