Jump to content

Is this possible?


chandler

Recommended Posts

Hi

I have this comment script it will be placed on a few pages, each are written to .txt file so there will be messages1.txt, messages2.txt, messages3.txt and so on,  is it possible using php to find out what was the last comment made and on what page, and then display this info on the main page some thing like

last 5 comments made:

made on page 3 17.07.2011 @ 15:00

made on page 2 16.07.2011 @ 15:00

etc

 

the comment script

		<?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
$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);

}
}

$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>";

?> 	



</div>

 

Many Thanks for you help

Link to comment
Share on other sites

It sounds like you're trying to save each comment in a separate text file. If that the case, is there a reason why you can't store all the comments in a single text file? With the single text file, you would just need to read in the entire file, count the number of entries as you go, and display the last line to get what you want.

Link to comment
Share on other sites

I thought this was the only way it could be done as I don't want the comments from page 1 mixing and being displayed with the comments from page 2 etc, I have been reading a bit about this tonight and its way over my head :( so I hope some one can help me..thanks

Link to comment
Share on other sites

Ok

I have setup a db, I have copied all the info ($mysql_host) etc and put it into a config.php file and I have placed an include config.php on the page. Now for setting up the query I have no idea how to do this, can you please help.

 

 

Thanks

Link to comment
Share on other sites

This is what I have so far, how do I put the data into the db and also display it?

<?php
require_once('config.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
$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);

}
}

$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>";

?> 	

 

Thank You

Link to comment
Share on other sites

Ok I have made the table,

 

// Create a MySQL table in the selected database
mysql_query("CREATE TABLE comments(
id INT NOT NULL AUTO_INCREMENT, 
PRIMARY KEY(id),
name VARCHAR(30))")
or die(mysql_error());  

echo "Table Created!";

 

 

Should I see any rows / columns id, names etc, cos at the moment I only see table comments without any option to browse it.

 

the complete code is, I know I have to remove some of the old code if any one can show me what I should remove it would be a big help ..thanks

 

<?php
require_once('config.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
$comments .=  file_get_contents('messages.txt'); // now we get all the existing comments and append it to the $comments variable.


// Create a MySQL table in the selected database
mysql_query("CREATE TABLE comments(
id INT NOT NULL AUTO_INCREMENT, 
PRIMARY KEY(id),
name VARCHAR(30))")
or die(mysql_error());  

echo "Table Created!";



// 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

I think I am close but can some one look this over, I'm not getting all the data from the db, I get the comment but no name?, and I am unsure if the data is coming from the db or the txt file.

also I have lost all the css now how do I get that back?

 

here is the code

<?php
require_once('config.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
$comments .=  file_get_contents('messages.txt'); // now we get all the existing comments and append it to the $comments variable.


// Insert a row of information into the table "example"
mysql_query("INSERT INTO comments 
(message, message2) VALUES('$message', '$message2' ) ") 
or die(mysql_error());  


}
}

$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

// Retrieve all the data from the "example" table
$result = mysql_query("SELECT * FROM comments")
or die(mysql_error());  

// store the record of the "example" table into $row
$row = mysql_fetch_array( $result );
// Print out the contents of the entry 
echo "<div id=\"census41_messages\">";
echo "Name: ".$row['mesaage'];
echo "Message: ".$row['message2'];
echo "</div>";

?> 	

 

Thanks

Link to comment
Share on other sites

mysql_fetch_* functions return one row at a time. So when your query returns more than one row you need to use a while loop to get all the results.

while($row = mysql_fetch_assoc( $result ))
{
    // Print out the contents of the entry 
    echo "<div id=\"census41_messages\">";
    echo "Name: ".$row['mesaage'];
    echo "Message: ".$row['message2'];
    echo "</div>";
}

 

Now that yo're using a database you no longer need these two lines.

// 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
$comments .=  file_get_contents('messages.txt'); // now we get all the existing comments and append it to the $comments variable.

 

As your code stands now it should be saving and retrieving the posted comments from your database. You now need to validate/sanitize your user input to prevent SQL Injection/XSS attacks. Have a read of this four part guide for writing secure PHP code.

Link to comment
Share on other sites

Hi thanks for that, I was just looking that up,  my problems now are the same message is repeated and there is a huge space between the messages can you see from the code why its doing this?  Thanks

 

here is the new code

<?php
require_once('config.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');






// Insert a row of information into the table "example"
mysql_query("INSERT INTO comments 
(message, message2) VALUES('$message', '$message2' ) ") 
or die(mysql_error());  


}
}

$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

// Retrieve all the data from the "example" table
$result = mysql_query("SELECT * FROM comments")
or die(mysql_error());  

// store the record of the "example" table into $row
while($row = mysql_fetch_assoc( $result ))
{
// Print out the contents of the entry 
echo "<div id=\"census41_messages\">";
echo "<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>";	
echo "</div>";
}
?> 	

Link to comment
Share on other sites

I cannot see why it is repeating messages/displaying large gaps. What I think is happening is you have a lot of duplicate/blank entries within your comments table.

 

You will want to clear the contents of your comments table now. To clear the contents of the table run the following query in PHPMyAdmin

TRUNCATE TABLE comments

 

Then try posting a couple of new comments and see if does it again.

Link to comment
Share on other sites

after doing "TRUNCATE TABLE comments" all the comments were removed, but I am still getting the duplicated comment and the space.

 

I guess this so I am sure it got something to do with this, do I need .$row ? if so how do I do that I did try kept giving errors

 

echo "<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>";	

Link to comment
Share on other sites

I thought this was the only way it could be done as I don't want the comments from page 1 mixing and being displayed with the comments from page 2 etc, I have been reading a bit about this tonight and its way over my head :( so I hope some one can help me..thanks

 

If you want to know for which page a comment is for, then put the page number on the same line as the comment.  Easy as that.

 

Wow, I didn't realize there was another page.. so I guess you went the db route.

Link to comment
Share on other sites

after doing "TRUNCATE TABLE comments" all the comments were removed, but I am still getting the duplicated comment and the space.

 

I guess this so I am sure it got something to do with this, do I need .$row ? if so how do I do that I did try kept giving errors

 

echo "<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>";	

Ooops. I didn't read your code properly earlier change the while loop to

while($row = mysql_fetch_assoc( $result ))
{
    $message  = $row['message'];
    $message2 = $row['message2'];

    // Print out the contents of the entry 
    echo "<div id=\"census41_messages\">";
    echo "<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>";	
    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.