Well, I made this guestbook script, and it worked fine when the comments were just getting stuck on the end of the file, but then I decided it would be a lot better if the comments were sorted newest-first instead. The logical way to do this, I guessed, would be to store the commments in a seperate text file, and stick them on the beginning instead of the end. However, my code doesn't work right: When a new comment is posted, the new comment appears like so:
(ALL older comments)
(New comment)
(ALL older comments AGAIN)
Anyway, here's the code I'm using. NOTE: Before using this, you have to make a file called "talk.txt", with a file length of more than 0 bytes,

<html>
<head>
<style>
body{ background-color:white; color:black; font-family:verdana;
}
a:link{ color: red;
}
a:visited{ color: red;
}
a:hover{ color: red;
}
a:active{ color: red;
}
input {
	border: 1px solid #333333;
	background-color: #FFFFFF;
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 8pt;
	color: #000000;
	padding: 0px;
}
textarea {
	border: 1px solid #333333;
	background-color: #FFFFFF;
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 8pt;
	color: #000000;
	padding: 0px;
}
</style>
<title></title>
</head>
<body>
<font size="-2">
<?
if ($nm==null){
?>


<form action='talk.php' method='post'>
name:<br><input type='text' name='nm' border="0"><br><br>
comment:<br><textarea style='height:100;width:130' name='co'></textarea><br><br>
<input type='submit' value='post'></form>


<?
}else{
$nm = strip_tags($nm);
$co = strip_tags($co);
$filename = 'talk.txt';
$fp = fopen($filename, "r+");
$blah = fread($fp, filesize($filename));
$write = fputs($fp, '<b>' . $nm . ' </b> (' . $REMOTE_ADDR . ')<br>' . $co . '<br><br>' . $blah);
fclose($fp);
?>
<meta http-equiv="refresh" content="0">
<?
}
?>
<br><br><br>Comments:<br><br>
<?
$filename = 'talk.txt';
$fp = fopen($filename, "r+");
echo fread($fp, filesize($filename));
fclose($fp);
?>

So, someone want to help?
EDIT: Sorry, I clicked submit twice, somebody delete ONE of the threads.

    You can delete it yourself by going EDIT

    Thanks in advance
    regards

      You can delete it yourself by going EDIT

      Thanks in advance
      Regards

        So, does anyone know what's wrong with my PHP here?

          Yes there are lots of people who can solve that in a flash of a second. But you need to read this first.

          Thanks in advance
          Regards

            OK, I tried searching the web, to no avail. I have no idea what's wrong. I started learning PHP last week. This is the "Newbies" forum. I'm a newbie (or, according to that site, a "luser"). Somebody help me 🙁.

              You can easily make the changes you want by modifying this line:

              $write = fputs($fp, '<b>' . $nm . ' </b> (' . $REMOTE_ADDR . ')<br>' . $co . '<br><br>' . $blah);

              This is where you are writing the data back to the file. Just restructure which information you want on the top of the file to get written first, and the older data to get written last.

              Also, you are writing back the file pointer, but you should be writing back the $blah (contents) here me-thinks...

                Create an array for the posts (load them from the db) then sort them with the date from newest to oldest

                use the sort array function

                  This may work...

                  <?php
                  // file
                  $file = 'talk.txt';
                  
                  // if file doesn't exist, try and create it:
                  if(!file_exists($file)){
                  	$fp = fopen($file, 'w');
                  	$close = fclose($fp);
                  }		
                  ?>
                  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                  <html xmlns="http://www.w3.org/1999/xhtml">
                  <head>
                  <title>Guestbook</title>
                  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
                  <style>
                  body{ background-color:white; color:black; font-family:verdana; font-size:10px;}
                  a:link{ color: red;}
                  a:visited{ color: red;}
                  a:hover{ color: red;}
                  a:active{ color: red;}
                  input {
                  	border: 1px solid #333;
                  	background-color: #fff;
                  	font-family: Verdana, Arial, Helvetica, sans-serif;
                  	font-size: 8pt;
                  	color: #000;
                  	padding:0px;
                  }
                  textarea {
                  	border: 1px solid #333;
                  	background-color: #fff;
                  	font-family: Verdana, Arial, Helvetica, sans-serif;
                  	font-size: 8pt;
                  	color: #000;
                  	padding: 0px;
                  }
                  </style>
                  </head>
                  
                  <body>
                  <?php
                  // if the form has not been submitted:
                  if (!isset($_POST['submit']))
                  {
                  ?>
                  <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
                  name:<br />
                  <input type="text" name="nm" border="0" />
                  <br />
                  <br />
                  comment:<br />
                  <textarea style="height:100;width:130" name="co"></textarea>
                  <br />
                  <br />
                  <input type="submit" value="post" name="submit" />
                  </form>
                  <?php
                  }
                  else {
                  	if(empty($_POST['nm']) || empty($_POST['co']))
                  	{
                  		echo 'Please go back and fill out the form completely.';
                  		exit;
                  	}
                  	else 
                  	{
                  		// strip tags
                  		$nm = strip_tags($_POST['nm']);
                  		$co = strip_tags($_POST['co']);
                  
                  	// file
                  	$oldcontent = file_get_contents($file);
                  	$newcontent = '<b>' . $nm . ' </b> (' . $_SERVER['REMOTE_ADDR'] . ')<br />' . $co;
                  	$newcontent .= "\n" . '<br /><br />' . $oldcontent;
                  	$fp = fopen($file, 'w+');
                  	$write = fwrite($fp, $newcontent);
                  	$close = fclose($fp);
                  	// if success
                  	if($write)
                  	{
                  ?>
                  <meta http-equiv="refresh" content="0">
                  <?php
                  		}
                  		// else error
                  		else {
                  			echo 'Sorry. Your comments were not saved. Please try again.';
                  		}
                  	}
                  }
                  ?>
                  <p style="padding-top:15px;">Comments:</p>
                  <p><?php
                  // Read file, comments:
                  $fp = fopen($file, 'r+');
                  while (!feof($fp))
                  {
                  	$buffer = fgets($fp, 4096);
                  	echo $buffer;
                  }
                  $close = fclose($fp);
                  ?></p>
                  </body>
                  </html>
                  

                  I only tested it for a few minutes....

                    Wow. Thanks a lot, you guys are amazing 🙂. Works perfectly.

                      Write a Reply...