...

View Full Version : flat-file cms: make new posts go to the top?



RyanRyan
03-25-2007, 04:46 AM
I need some help with a cms I am making, i cant figure out how to make the old news go down and put the newest news at the top. can anybody help?

<?PHP
$time = date("F jS Y, h:iA");
$name = $_POST["name"];
$title = $_POST["title"];
$text = $_POST["text"];

$data = fopen('show_news2.php', "w+");

fwrite($data, "<center><strong>$title</strong></center><br/>
$text <br/>
<center><em>Posted by: $name, on $time</em></center><br/><br/>");
fclose($data);
echo 'News Updated, <a href="http://tackypenguin.com/index.php">Home page</a>, or go <a href="index.php">Back</a>';
?> thats what i have so far

iLLin
03-25-2007, 04:54 AM
$data = fopen('show_news2.php', "r+");


w+ Truncates the whole file and starts over. r+ is what your looking for.

Armondo
03-25-2007, 04:57 AM
i'm sitting right next to him...what he is trying to say is that he wants to make the older posts move down and the new post be placed on top. look at my website: www.flashanims.com do you see how the newest posts are at the top? that is what he wants. and i think r+ just goes to the top and then writes over it.

RyanRyan
03-25-2007, 04:58 AM
Okay i tried it and it wrote from the top, but it wrote over the old news instead of pushing it down so there was only a part of the old news below the new news, the rest was written over

iLLin
03-25-2007, 04:58 AM
http://us2.php.net/manual/en/function.fopen.php

RyanRyan
03-25-2007, 05:01 AM
There has to be a way. i tried to look over cutenews code to see but i couldnt find any

Armondo
03-25-2007, 05:01 AM
hmmm.......

iLLin
03-25-2007, 05:03 AM
Ok I see that append only writes to the bottom and everything else overwrites. How about this.

Open the file in 'r' mode.
Read it and put the contents in a variable.
Append your new content to the beginning of the read variable
Write it back to the file.

See if that works for ya.

RyanRyan
03-25-2007, 05:09 AM
like this?
<?PHP
$time = date("F jS Y, h:iA");
$name = $_POST["name"];
$title = $_POST["title"];
$text = $_POST["text"];
$data2 = fopen('show_news2.php',"r");

$data = fopen('show_news2.php', "w");

fwrite($data, "<center><strong>$title</strong></center><br/>
$text <br/>
<center><em>Posted by: $name, on $time</em></center><br/><br/>", $data2);
fclose($data);
fclose($data2);
echo 'News Updated, <a href="http://tackypenguin.com/index.php">Home page</a>, or go <a href="index.php">Back</a>';
?>

iLLin
03-25-2007, 05:16 AM
$data2 needs to be "read"



$filename = "show_news2.php";
$handle = fopen($filename, "r");
$old_contents = fread($handle, filesize($filename));

$new_contents = "<center><strong>$title</strong></center><br/>
$text <br/>
<center><em>Posted by: $name, on $time</em></center><br/><br/>";

$write = $new_contents.$old_contents;

$handle2 = fopen($filename, "w");
fwrite($handle2, $write);

//close your opens... and maybe add in some error checking?

RyanRyan
03-25-2007, 05:22 AM
Thank you, I have been tryin to figure out how to forever THANK YOU. what is your website?

iLLin
03-25-2007, 05:25 AM
Your welcome. I run quite a few but my personal one is http://www.myarmedforces.com.

Glad to help enjoy!

Armondo
03-25-2007, 05:26 AM
yippe ryan you FINALLY GOT THIS WORKING. you don't even want to know how long he has been bugging me about this.

RyanRyan
03-25-2007, 05:28 AM
nice site thanks once again



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum