Jump to content

comment script question!


chandler

Recommended Posts

Hi

I have this comment script  I am using on several pages. 

What I want is for the newest comment to appear on top, and if its possible I would like something on the main page telling the users where the last comment was made for example: last comment was made on page 3 17.07.2011 @ 15:00

I am rubbish at php so any help / links to help with the above would be much appreciated.

 

<?php
session_start();
?>

<?php

if (isset($_POST['message']))
{

if (isset($_SESSION['token']) && $_POST['token'] == $_SESSION['token'])
{
$message = htmlentities($_POST['message']);
$message2 = htmlentities($_POST['message2']);
$message = stripslashes($_POST['message']); 
$message2 = stripslashes($_POST['message2']); 


$fp = fopen('messages.txt', 'a');
fwrite($fp, "<div id=\"comments_box\"><div id=\"comment_name\"><p>$message <em>Says:</em></div><br />$message2</p></div>");
fclose($fp);
}
}

$token = md5(uniqid(rand(), true));
$_SESSION['token'] = $token;



?>


<form id="contFrm" method="POST">
<input type="hidden" name="token" value="<?php echo $token;?>" />
<label><span class="required">*</span> Full Name:</label>
<input type="text" class="box"  name="message"><br />
<label><span class="required">*</span> Message: </label>
<textarea name="message2" id="message" cols="25" rows="8"></textarea><br />
<input type="submit" class="button" value="Submit">
</form> 




<?php
echo "<div id=\"census41_messages\"> ";
readfile ('messages.txt');
echo "</div>";
?> 	

 

Many Thanks

 

 

Link to comment
Share on other sites

To insert the latest post so it at the top you'll need to change the way your script adds the comments to messages.txt. As it is now your appending the latest comment to the file, which means the latest comment will always be added to the end of file.

 

In order to add the latest comment so it at the top of the file you'll first need to get all the comments that is in messages.txt and then prepend the latest comment. Which will be like this:

// add the newest comment to the start of the file
$comments  = "<div id=\"comments_box\"><div id=\"comment_name\"><p>$message <em>Says:</em></div><br />$message2</p></div>"; // this is the new comment to be added
$comments .=  file_get_contents('messages.txt'); // now we get all the existing comments and append it to the $comments variable.

// write all the comments back to the file with the newest comment first
$fp = fopen('messages.txt', 'w');
fwrite($fp, $comments);
fclose($fp);

 

To add the date look into using the date function.

Link to comment
Share on other sites

Thanks for that here is the update code, but what I get now is the newest post replacing the last post. Have I done something wrong?

 

 

<?php



if (isset($_POST['message']))
{

if (isset($_SESSION['token']) && $_POST['token'] == $_SESSION['token'])
{
$message = htmlentities($_POST['message']);
$message2 = htmlentities($_POST['message2']);
$message = stripslashes($_POST['message']); 
$message2 = stripslashes($_POST['message2']); 

ini_set('date.timezone', 'Europe/London');



// add the newest comment to the start of the file
$comments  = "<div id=\"comments_box\"><div id=\"comment_name\"><p>$message <em>Says:</em></div><div id=\"comment_date\">" . date ("D, M d, Y, g:i a") . "</div><br />$message2</p></div>"; // this is the new comment to be added
$comnents .=  file_get_contents('messages.txt'); // now we get all the existing comments and append it to the $comments variable.

// write all the comments back to the file with the newest comment first
$fp = fopen('messages.txt', 'w');
fwrite($fp, $comments);
fclose($fp);

}
}

$token = md5(uniqid(rand(), true));
$_SESSION['token'] = $token;



?>


<form id="contFrm" method="POST">
<input type="hidden" name="token" value="<?php echo $token;?>" />
<label><span class="required">*</span> Full Name:</label>
<input type="text" class="box"  name="message"><br />
<label><span class="required">*</span> Message: </label>
<textarea name="message2" id="message" cols="25" rows="8"></textarea><br />
<input type="submit" class="button" value="Submit">
</form> 




<?php
echo "<div id=\"census41_messages\">";
readfile ('messages.txt');
echo "</div>";

?> 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.