Comment Reply System
#1
Posted 03 February 2007 - 10:38 PM
I've worked on my own comment systems and what-not that may be similar to that of IPB's in form of use, functionability and flexibility atleast. I was wanting to develop a reply part, like you'd see on dA or some blogs where the reply appears just below the original comment, but with a slight indent.
The only way I found this able to happen is to recursively run a SQL command to find any comments with a reply_id equal to the current comment. While this is easy, I'm wondering if it is at all practical, since this easily runs up to possibly hundreds of queries on just stupid comments! I can only imagine how much of a work-load this must be on dA with how complex and deep their comment/reply system goes (I literally see replies to replies of replies of replies of replies of replies..... and so on until the final reply is nearly on the complete far right of the page with a very small size because of the style).
Is there another, and more practical way without recursion of SQL queries for every single comment retrieved?
#2
Posted 03 February 2007 - 10:43 PM
SELECT * replies WHERE post_id = '$post_id'
or am i missing something specific that you're wanting to do?
Edited by SpatialVisionary, 03 February 2007 - 10:44 PM.
#3
Posted 04 February 2007 - 03:07 PM
So I guess everyone else has come to pretty much the same conclusion as I then.
Ok thanks guys, was just making sure there wasn't some other way I wouldn't have been able to think of before I got too deep into it all.
#4
Posted 04 February 2007 - 05:26 PM
"SELECT c1.id, c1.message, (SELECT COUNT(c2.id) FROM COMMENTS c2 WHERE c2.parent_id = c1.id) as count FROM comments c1"
if count >= 1 run another query blabla, you get the idea
edit: never knew about the sql bbcode
Edited by Av-, 04 February 2007 - 06:34 PM.
#5
Posted 04 February 2007 - 07:37 PM
Maybe a small example?
#6
Posted 05 February 2007 - 12:18 PM
Actually, would it not be easier to get all rows at once, sort them (parent->child->child child
#7
Posted 05 February 2007 - 04:33 PM
In theory it all makes sense to me now, but the fun part will be trying to actually do that sorting. :heh:
Any ideas on how to do this?
I'll draft an example for anyone who is more of a visual learner, lol.
Table `comments` _____________________________ | c_id | p_id | p_lvl | txt | ---------------------------------------- | 1 |NULL | 1 | 1st comment | | 2 | 1 | 2 | Reply to #1 | | 3 | 2 | 3 | Reply to #2 | | 4 | 1 | 2 | Reply to #1 | | 5 |NULL | 1 | 2nd comment| | 6 | 2 | 3 | Reply to #2 | -----------------------------------------c_id - Comment id (auto_increment)
p_id - Parent id (NULL if root comment)
p_lvl - Level of nesting (how deep is this comment in the whole schema)
txt - Actual text of the comment obviously
(ASCII art is hard on IPB...)
Then when this is all sorted and displayed, should look like this.
#1: 1st comment
-----#2: Reply to #1
----------#3: Reply to #2
----------#6: Reply to #2
-----#4: Reply to #1
#5: 2nd comment
Hope that helps someone understand what I was thinking, lol.
Now the fun part is constructing a SQL query that should be able to sort it like this...
All I can think of at the moment now is to use this.
SELECT c_id, p_id, p_lvl, txt FROM comments ORDER BY c_time, p_id(The c_time would be a column with the timestamps of each comment)
Then the thing is, how to use arrays to sort the replies and pair them with their respective parent comments...
Arg, if only I knew how dA and P2L did it! Lol.
Edited by Demonslay, 05 February 2007 - 04:37 PM.
#8
Posted 05 February 2007 - 05:37 PM
All your top level comments have a parent ID of 0, and you make a function to pull out all of the comments depending on parentID.
As you loop through the comments, one by one you look to see if it has children, either by doing a query to see if it has children, or storing in the current row if it has children, and if it has children, then you call the function again witin itself with the current rows ID as the parent ID.
A quiick example:
<?php
function recurse( $parent_id ) {
$select = mysql_query( "SELECT * FROM `comments` WHERE `parent_id` = '".$parent_id."' " ) or die( mysql_error() );
if( mysql_num_rows( $select ) > 0 ) {
while( $row = mysql_fetch_assoc( $select ) ) {
if( $row['has-children'] == 'y' ) { #If the current comment has children
recurse( $row['id'] ); #Call the function again with the current ID to get all its children.
} else {
$display .= 'Comment with ID '.$row['id'].' with parent ID '.$parent_id.'<br />';
}
}
}
return $display;
}
#To get it going you call the function with the parent ID of the top level categories, for example I am using 0 as top level parent ID's
echo resurse( '0' );
?>
Be careful as you can get yourself stuck in a infinitave loop if your not careful. But thats the bascis to it, doesn't really get too much harder then that.
Matt
#9
Posted 05 February 2007 - 09:30 PM
Thanks Matt.
I'm starting to wonder about Av-'s method of a 'sub-query' nested inside of another query though... I've never seen that, and I'm starting to wonder if it is possible even...
#10
Posted 06 February 2007 - 05:15 AM
So if you have a sub query on the main select query you are limited to showing 1 level of sub comments.
By using the recursion you are not limited, as the script will run through and keep checking if the current comment has children, and if it does it finds those children out, if not it carries on and will find all the comments that are linked.
Your welcome
Matt
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users
