By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,594 Members | 3,552 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,594 IT Pros & Developers. It's quick & easy.

PHP/MySQL Forum Comment Reply System Issues...

helimeef
P: 77
I designed a little PHP/MySQL forum (just for fun), and I have commenting set up and top threads on the homepage and whatnot. I'm relatively new to PHP, and this is the biggest project I've done, but the only problem is that I can't think up a way to let people reply to an already-posted comment...

Anyway, I have a MySQL database called "forum", with three tables; "comment", "thread", and "user". I had the idea of creating a new field in the "comment" table called "reply-to_x " (each comment posted has a specific ID, and x being the comment that it replies to). But I have no idea how I would get PHP to know to place reply comments under the comments they reply to. Here is the code in showThread.php that looks through the database for comments:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     mysql_connect('localhost','root','fakepass') or die(mysql_error());
  3.     mysql_select_db('forum') or die(mysql_error());
  4.     $result = mysql_query('SELECT * FROM comment WHERE thread_id='.$t);
  5.     while($row = mysql_fetch_array( $result )) {
  6.         echo '<div class="comment" id="'.$row['id'].'">'."\n";
  7.         echo '<p class="handle">'.$row['name'].'</a></p>'."\n";
  8.         echo '<p>'.$row['message'].'</p>'."\n";
  9.         echo '</div>'."\n";
  10.     }
  11.     mysql_close('forum');
  12. ?>
  13.  
Where $t is the showThread.php?t=x part of the address (t being the thread ID), $row['id'] is the unique comment ID (for that thread), $row['name'] is the author of the comment, and $row['message'] is the comment itself.

Any ideas on how to make reply-tos possible?

Thanks,
Steven
Sep 20 '07 #1
Share this Question
Share on Google+
5 Replies


pbmods
Expert 5K+
P: 5,821
Heya, Steven.

Please use CODE tags when posting source code:

[CODE=php]
PHP code goes here.
[/CODE]

Create a `topics` table and a `posts` table. Add a topic ID to each record in `posts` so that you can group each post by topic.

When displaying a topic, simply fetch all posts that match the topic ID, ordered by date posted in descending order.
Sep 30 '07 #2

helimeef
P: 77
Heya, Steven.

Please use CODE tags when posting source code:

Expand|Select|Wrap|Line Numbers
  1. PHP code goes here.
  2.  
Create a `topics` table and a `posts` table. Add a topic ID to each record in `posts` so that you can group each post by topic.

When displaying a topic, simply fetch all posts that match the topic ID, ordered by date posted in descending order.
lol you answer every single thread. Sorry about the code tags, I was new to the forum. And I figured out a way to do the reply, in fact I now have a fully functional forum, and now I'm working on the 3rd build! YAY!
Dec 25 '07 #3

Markus
Expert 5K+
P: 6,050
lol you answer every single thread. Sorry about the code tags, I was new to the forum. And I figured out a way to do the reply, in fact I now have a fully functional forum, and now I'm working on the 3rd build! YAY!
We'd love to see it :)
Dec 26 '07 #4

helimeef
P: 77
We'd love to see it :)
I'd love to show you it, but I don't really have any servers (except for my clients') that I could use as a sandbox right now, only localhost...
I'm also working on a blog and an online store, but those are barely started...
Dec 26 '07 #5

P: 2
Hi,
I needed to answer exactly the same question when writing a php and mysql comment and reply system for my webpages.
For the first attempt I decided to restrict my design to one level of reply. Allowing replies to comments but not replies to replies. I use a 'reply_to' field just as you propose: any reply has a reply_to field which contains the unique id of the comment being answered. For any commment this field is NULL. I also used a 'page_id' field but this could be a 'thread_id' if needed. The MySQL query which places replies after the relevant comments can then be based around:
Expand|Select|Wrap|Line Numbers
  1. ... SELECT ... ORDER BY COALESCE ( IdReplyTo, IdComment) DESC';
  2.  
This returns IdReplyTo - unless that field is NULL in which case it returns IdComment. This makes sure that for, say comment 23, where IdReplyTo = NULL, the comment is returned then all its replies, IdRelplyTo =23which is what is needed.
For more info see: http://www.jgdprojects.co.uk/codingcomments.htm
Oct 5 '13 #6

Post your reply

Sign in to post your reply or Sign up for a free account.