Simple "Add a Comment" feature

To make one of my websites more interactive, I would like to add an “Add a Comment” feature at the end of my newsletter articles.

How would I go about building something like this from scratch? (I prefer learning and building things from scratch…)

Debbie

This is like “interactive website 101”, so you can pick and choose from the past 11 years worth of PHP tutorials all over the web.

[list][]Create a database table to hold the comments. It needs at least 4 columns (id, article, name or email, comment text).
[
]On your article webpage, select all the rows for that article from the table and display them.
[*]Put a form on the page, and create a script that reads this form, then performs an INSERT query to save new comments to the table.[/list]

Need a step-by-step tutorial for doing this? BuildYourOwn DatabaseDrivenWebSite UsingPHP*&*MySQL, 4th Edition - SitePoint Books

Any interest in using a ready-made solution? Just drop in Discover your community - DISQUS

Your response implies that I need to have my articles in a database as well? :-/

Right now, all content on my website is hard-coded XHTML.

Can I do what you were suggesting with hard-coded articles but using a database for the comments portion?

Debbie

Yes. Your articles don’t need to be in a database, though it’d be an excellent time to put them there instead of editing every webpage to include your new commenting code.

Also by having the articles in a database it makes them easier to search by all users, you could search for example:

  • all articles posted between Jan 2010 and Dec 2010
  • All articles posted during May 2010 and in category “cooking”

You would have a categories table, and a categories_articles table which would act as “linking” table. If an article was in 3 categories then there would be three rows in the linking table, all three would have the same article number but each would have a different category number

If your article html system uses a naming convention such as ;

/articles/my-little-article.php

Then you could simply have a matching comments system ;

/comments/my-little-article.php

That could be a simple text file into which you append lines as comments are made. Your biggest problem will likely be trying to keep your file system secure and protecting your website viewers from hostile xss attacks.

Reading and writing from files can easily be looked up in the manual. Tutorials abound too.

Defining clearly on your GUI that you do not allow html markup in your comments will also ease your passage. If you do agree with that, upon Filtering the input, if you find any kind of tag, abort the operation - don’t bother trying to cleanse the input.

You should then learn a bit about file access and some security issues, then, when it suits you, move to storing things in databases and this knowledge will be useful.

I am not saying don’t use a database, I am responding to the spirit of the question in your OP (original post).

Ostensibly then, in a php “footer” which each article should evoke, you will have a line of code which does something similar to:


$the_file = '/comments/' . $article_name . '.php' ;

if ( file_exists( $the_file )) {
echo "<hr />Comments<hr />";
include ($the_file);
// or file_get_contents( $the_file )
}

The question is how do you retrieve and relate and map the $article_name from the article to the associated comments? If you cannot work out how to do this using your legacy system, then it might well be best to simply learn about using a database now.

Here is my current workflow for content on my websites…

1.) Compose an article in OpenOffice.

2.) Edit the content, layout and “wordsmith” things so it is just right.

3.) Paste the content into NetBeans.

4.) Manually add HTML tags.

5.) Create any new styles needed and perform any additional “Wordsmithing” needed.

6.) Proof article and post it.

This may seem like a horribly old-fashioned way to do things, but I don’t find adding the tags/styles that much additional work, and it allows me to keep my code and layout super clean and simple.

(The thought of relying on something like Dreamweaver makes me cringe…)

How does everyone else do things, and how would that relate to putting my articles into a database?

Debbie

Good idea, but since I have less than 10 articles, there isn’t much of a need for that so far. (I’m focusing on quality articles versus random babbling like most blogs. To write one article, I typically spend 8-12 hours, so it’s a LOT of work and a slow process.)

Debbie