[PHP] fclose help

hi…

i’m trying to sort out this text area that adds text to a textfile called test.txt…im just using the one text box so i want it so when you hit enter to start a new line in the text box it inserts a linebreak when you’ve submitted the text into the textfile…

i also want each post to just wipe over the last…so theres only ever one lot of text in the text file at a time…:slight_smile: thanks

heres the code im using for the php file to add text:

<?php
$filename = 'test.txt';
$somecontent = '<table width="90"  border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="6" align="right" valign="middle"></td>
    <td width="84" align="left" valign="top"><strong>' . date('d.m.y') . '</strong></td>
  </tr>
  <tr>
    <td align="right" valign="top">-</td>
    <td align="left" valign="top">' . nl2br($_POST['updates']) . '</td>
  </tr>
</table>';

if (is_writable($filename)) {
   if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
   }
   if (fwrite($handle, $somecontent) === FALSE) {
       echo "Cannot write to file ($filename)";
       exit;
   }
   
   echo "Success, Updates were changed!";
   
   fclose($handle);
                   
} else {
   echo "The file $filename is not writable";
}
?>

and if you want to see the text box…its here :

http://www.band-central.net/atreyu/test/index.php

and the post gets added to this page

http://www.band-central.net/atreyu/home.php

on the left side in the ‘updates’ box

maybe this will help


$text_post = nl2br($somecontent);

use $text in the fwrite(…) function instead of $somecontent

or change this line
<td align=“left” valign=“top”>’ . ($updates) . ‘</td>
to
<td align=“left” valign=“top”>’ . nl2br($_POST[‘updates’]) . '</td>

try to use the $_POST array instead of relying on register_globals being ‘on’ in php.ini. That way your script will still work if you one day have to move it to another server or give it to someone.

ok…thats worked, one more thing…

how would i get the posts to list in order of the posted, newest-oldest…with only two being posted, then the oldest one gets deleted when a new one is posted :slight_smile:

thanks

That’ll be hard because you store your items as HTML. You have to read the entire file and somehow separate each entry from the others. Then you have to output the last two in reverse order.

If you’d stored them in the file without the HTML - or in a database - it’d be much easier.

whatabout just storing them in reverse order? would that be possible still storing them as html?

i have no clue when it comes to databases :frowning:

If you store them as HTML in reverse order, you still have to figure out where the second entry ends, so you can chop it off there.

You could put some sort of marker between the entries. An HTML comment for instance. But it’s still a bit of a kludge, IMO.