ware repair - fargo computer repair
Ware Repair closed August 28th, 2020

Web Development - How to quote comments in a forum

Web Development - How to quote comments in a forum

Written by Diane Diane Ware - September 25, 2017 in Programming

I designed my own forum website, using a few online tutorials and other online sites. The programming responsible for the functionality of this forum website is primarily PHP and MySQL, so as to maintain and manipulate database tables of forum categories, forum topics, and the posts of these topics, as well as tables for users and their accounts.

But now, after designing the main functionality of this forum, I have hit upon a dilemma. How do you enable users to quote posts from previous comments?

At first blush, it seems and sounds quite straight forward. But in actuality, it is a bit more complicated than that.

There are probably numerous ways to go about solving this dilemma, but in this blog I explain how I am tackling it so far.

The steps involved: 

1. The first user posts a comment.

2. A second user wants to quote that comment, along with posting their own comment too.

3. The second user clicks on the 'Add quoted reply' button.

4. The first user's comment is sent to a PHP page that will handle the quote. On this page, the comment is wrapped in quotes like so (along with 'Originally posted by: username', though I left that out for now):

[quote]First user's comment.[/quote]

And then this is placed in a text editor.


5. The second user then adds a comment below the quoted part:

[quote]First user's comment.[/quote]

Second user's comment.

6. Once finished with the comment, the second user clicks the submit button and the entire comment, both the first user's comment with the quotes and the second user's new comment, is sent to the posts database.

7. Immediately the page then redirects back to the original forum topic. Once there, after going through a function I will describe shortly, it will look somewhat like this (to distinguish between the quoted comment and the new comment):

Now, here is when things get a bit tricky. What about if a third user comes along and wants to quote that entire post above? Some online forums will allow this. One forum website I am familiar with will allow up to four previous comments to be posted before they have a cutoff. For example, look at the image below: 

However, after consulting with a web development expert here at Ware Repair, we both agreed that this can lead to trouble, especially if the original post, or any subsequent posts, are very long. Of course, this problem can be eliminated if a word count limit is set on the text editor, but that would be the subject of another blog post, not the focus of this blog post. 

So what I decided upon was just to allow a one quote comment limit. In other words, if you see a comment on a topic thread that you want to quote, it can be quoted just one time. If after you post both the quoted comment and your new comment to the topic thread, and another, third user, then wants to quote this entire comment, the third user cannot. The  third user will only be able to quote your new comment (the second comment), and add their own new (third) comment. So only the second comment and the third comment will be present on the thread as a new post. 

Here are the steps: 

1. First, a user clicks on 'Add quoted reply' for a post the user would like to quote in their comment. The button click will send the user to another php page, post_reply: 

2. Then, on my post_reply php page, a function, checkquotes, is called before the quoted comment can be displayed in the text editor. This function will remove [quote][/quote] if it is second quoted comment, or add [quote][/quote] if it is a first time quoted comment. This function, checkquotes, will also add the name of the user who originally posted the comment needing to be quoted: 

3. This is how the 'First comment' appears in the text editor on the post_reply page: 

And then the user adds the 'Second comment': 

3. Once the user clicks on 'Post Your Reply', the comment goes to a php parse page, so the comment can be stored properly in the posts database table. Then, once done, the code on the parse page sends the user directly back to the topic thread where the user originally saw the comment they wanted to quote. This page, my view_topics php page, has another function. It is named quoteposttest, and wraps the quoted part in a colored html div section, while the new comment part is not placed in a div section. 

4. And now the final outcome of all of the above: 

Certainly, adjustments can be made in the functions to change the background color of the html div area, or to make your own [quote][/quote] pairs. Also, other lines of code could be added to the functions to prevent users from inputting their own quote blocks that will not work with either function. But this is what I have for now. 

For those interested in designing and developing a PHP/MySQL forum, I recommend the Tim Kipp forum tutorial on YouTube. And another good source is Adam Khoury's web development videos. 



Diane Ware

Manager - Tech Support

I am co-owner of Ware Repair and enjoy working with technology, web design and development, and embedded programming, especially that which applies to robotics. I also enjoy writing blogs and sci-fi novels on my free time.