basic problem.. appending to start of file!

I only started php yesterday but have programming experience but sorry if this is a very elementary thing!
I have a form which is filled out to update news.. i add the information to a file& it then puts this on a page but the problem is the oldest news is displayed first...
I use two files, one for old news & one for new news.

What i'm trying to do is:
first add the new news to the file,
then create an array with the old news information,
then add the old array to end of new file,
then copy new file to old file so it will all be old news next time.


$newfile = "comments.txt";
$oldfile = "oldcomments.txt";
$f_write = fopen($newfile, "a");
/adding the line into the file
fputs($f_write, $array);

//create array with c ontent of old file

$array2 = file($oldfile);

//add array to new file
for( $i=0; $i < count($array2); $i++ )
{
fputs($f_write, $array2[i]);
}
//delete old file before copy new one to it
unlink ($oldfile);

//copy new file to old file
copy($newfile, "oldcomments.txt");

//closing the file comments.txt
fclose($f_write);

Comments

  • I think the problem is how you display them to a webpage
    [purple]a tiny message board demo,in 5 minutes
    ">



    <?$cmt=date("Y/m/d H:i").'<br>'.$_POST['Comment']."
    ";
    //no need 2 files,only one ;)
    $n='comment.txt'; $f=fopen($n,'a'); fwrite($f,$cmt); fclose($f);
    $f=fopen($n,'r'); $cmts=explode("
    ",fread($f,filesize($n))); fclose($f);
    $i=count($cmts); while($i--)echo $cmts[$i-1].'

    ';?>[/purple]

  • Thanks....
    It doesn't make complete sense to me right now but i'll take it home and study it for homework :p
    Cheers,
    Saz
    ***


    : I think the problem is how you display them to a webpage
    : [purple]a tiny message board demo,in 5 minutes
    : ">
    :
    :

    : <?$cmt=date("Y/m/d H:i").''.$_POST['Comment']."
    ";
    : //no need 2 files,only one ;)
    : $n='comment.txt'; $f=fopen($n,'a'); fwrite($f,$cmt); fclose($f);
    : $f=fopen($n,'r'); $cmts=explode("
    ",fread($f,filesize($n))); fclose($f);
    : $i=count($cmts); while($i--)echo $cmts[$i-1].'<p>';?>[/purple]
    :



Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories