Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a comment system to my website [closed]

Hey everyone, I'm trying to add some type of comment system to my website for the news that I post on the main page. What I would like it to do is have anybody comment on it (they don't need to login). The comment submission form just required a Name and comment. What would be the simplest way to do this? It would also be nice to have it display how many comments there currently are on the news post. I dont need anything fancy. Thanks!

like image 269
MJ93 Avatar asked Dec 10 '10 19:12

MJ93


2 Answers

If you don't want to code your own comment system, you can use http://disqus.com/

like image 73
Buddy Avatar answered Nov 01 '22 15:11

Buddy


There are many ways to do that... i try to give you a simple idea (without use patterns or complex method):

1) Create a Database Table to contain comments, i suggest these fields:

id (integer)
news_id (fk to id of the news) 
date (i.e. Timestamp)
name (varchar(30... or less))
message (text)  

2) In your frontEnd page add a form composed of 4 fields: news-id : self-explanatory name : input text field message : textarea captcha: (to avoid bot completion) i suggest you recaptcha.

<form action="add_comment.php" method="POST">
  <label for="name">Your Name:</label>
  <input type="text" id="name" name="name" />

  <label for="name">Your Comment:</label>
  <textarea id="comment" name="comment"></textarea>

  <input type="hidden" name="news_id" value="<?php echo $news_id?>"/>
  <input type="submit" value="Ok" name="save"/>
</form>

This form send data in POST to add_comment.php that need to implement these steps:

2.A) check if $_POST data exist

if(isset($_POST["save"])) 

It would be better check provenience of data (to be sure that are from your site).

2.B) If $_POST data exists,check mandatory fields and store error in some structure:

if( (trim($_POST["name"]) == "") and (strlen($_POST["name"]) < 5) ){
  $name_error = true;
}

2.C) If there aren't errors, save data to the database: - open db connection - assemble a query. Do not forget to wrap every variable into quotes and run it through mysql_real_escape_string (or use prepared statements) - run this query
- redirect to the current page

2.D) If there are errors, redirect to main page with a variable in get &error=1. In your main page check if this variable is set to define if you need to print some error messages. (better to stay on the same page and display errors as well as fill entered data (avoid xss scripting ))

3) Manage your main page adding a script to select comment from DB, here some steps:

3.A) For each news you print, get the id (or the unique key used to store news in db).

3.B) Perform a simple select query like this to get all comment for this news:

$query = "SELECT name,message from comments where id_news = '{$_newsid}' order by date DESC";

3C) For each comment you obtain with this query you can print data in this way :

<?php foreach($query_fetched_results as $comment):?>
<p class='name'><?php echo $comment['name'];?></p>
<p class='comment_body'><?php echo $comment['message'];?></p>
<?php endforeach;?>

4) Check number of comment is pretty simple, perform a count on data obtained from query at point 3B.

like image 20
MatterGoal Avatar answered Nov 01 '22 16:11

MatterGoal