• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
hmmm i wonder how i make this...

#1
[eluser]Tom Taila[/eluser]
Im designing a comment system and ive gotten to the point of where the user can comment and its all working except i also want users to be able to comment on a comment, if that makes sense. Sort of like facebook how if i comment on a friends page then another friend can comment on that post. I hope i explained myself well enough ive spent all day trying to figure this out, please help :S thanks

#2
[eluser]pickupman[/eluser]
This could be done similar to how categories are handled in cart packages. Add a comment parent id field to your DB. When a inserting a new comment into DB, if it is a reply comment add the comment parent id, otherwise 0 or something to designate it is a top level comment. This will allow you to create as many threads deep as you would need. As in someone replies to the reply, the parent id would be the initial parent ID of the first reply (not the initial comment).

Kind of confusing but I think you might understand it

-------
Example
-------
(Parent ID: 0 ID: 1) Hello Tom
(Parent ID: 1 ID: 2) Hello Joe
(Parent ID: 1 ID: 3) What have you been up to Tom?
(Parent ID: 3 ID: 4) Not much here just on the CI forums.
(Parent ID: 0 ID: 5) Anyone has a link to the user guide?
(Parent ID: 5 ID: 6) I've got the link for you.

#3
[eluser]Tom Taila[/eluser]
I'll be honest im not sure i understand this :S lool, maybe there is a tutorial i should read/watch to help me? if not could you please explain it in a lil more detail coz to be honest im baffled. I think i may understand the idea behind it having the capability to be infinitly many levels deep but i only want a max depth of 2, i.e. i do not want a comment on top of a comment on top of a comment.
lmao that was a really badly structured sentence but i think it makes sense, thanks for the help btw Smile
p.s. if i reply late ive gone to sleep its 4.10 am over here ! cheers bro

#4
[eluser]pickupman[/eluser]
Take a look with some fresh eyes tomorrow and it should make sense. Think about about a ecommerce site would work with categories. You have a level of parent categories to choose from usually displayed as tabs at the top. You then have dropdowns under the tabs for each subcategory. Blogs work the same way. Using the example I gave above ID: would be a comment_id field that is auto-incremented each time any comment is made. This is probably what you already have. You need to add a comment_parent_id field with something like mediumint(6) unsigned or something like that to your database. Now when inserting a new comment, if it is a reply to a previous comment, the comment_parent_id is set to the comment_id of original comment.

With that in mind, take a look at the example. The Parent ID: represents the id the comment is responding to. If the Parent ID: is 0, it is not replying but adding a new conversation. This means:
ID: 1 = New comment thread
ID: 2 = Responds to ID:1
ID: 3 = Responds to ID:1
ID: 4 = Responds to ID:3 (1 level deep)
ID: 5 = New comment thread
ID: 6 = Responds to ID:5

#5
[eluser]Tom Taila[/eluser]
damn i didnt get the chance to try this out today, but i will do definitly tomorrow, thanks for the help ill let u know how it goes

#6
[eluser]Tom Taila[/eluser]
I tried your idea and failed unfortunately, well actually thats not entirely true, i managed to make it so that a comment on a comment does take a parent_id which is equal to the parent comments id. That was no problem. The problems im having actually is to do with getting it on my webpage, right now my code for my commentpages view file looks like this:

Code:
<?php if(isset($all_comments)){
                    
                    foreach($all_comments as $row){
                        echo '<div class="comment_box_container">';
                        echo '<div class="comment_box_user_details">';
                        echo '<a href="#"><img src="http://profile.ak.fbcdn.net/hprofile-ak-sf2p/hs623.snc3/27387_864335610_9820_q.jpg" alt="topic1" class="user_picture" /></a>';
                        echo '<a href="#" class="user_name_profile_link">' . $row->user_name . '</a>';
                        echo '<p class="user_rank">' . $row->user_rank . '</p>';
                        echo "<p class='user_score'>User score:" . $row->user_score . "</p>";
                        echo "<p class='user_level'>Level:" . $row->user_level . "</p>";
                        echo "<p class='comment_time'>X Hours ago</p>";
                        echo "<p class='comment_score'>score:" . $row->score . "</p>";
                        echo "<a >id . "' class='vote_up_button'><img src='" . base_url() . "images/icons/vote_up.jpg'></a>";
                        echo "</div>";
                        echo "<div class='comment_content_div'>";
                        echo "<p class='comment_content'>" . $row->content . "</p>";
                        echo "</div>";
                        echo "<a href=" . base_url() . ">id . " class='delete_comment_button'>Delete</a>";
                        echo "</div>";
                        
                echo '&lt;form action="' . base_url() . 'index.php/comment/add_comment2/' . $row-&gt;id . '" method="post" class="comment2_form">
                      &lt;textarea name="comment" class="comment2_textarea"&gt;dimension test&lt;/textarea&gt;&lt;br />
                      <p>&lt;input type="submit" value="Post!" class="submit_comment2_button"/&gt; | &lt;input type="submit" value="Post as anonymous!" class="submit_comment2_anonymous_button"/&gt;&lt;/p>
                        &lt;/form&gt;';
                        }
                    }
                    else {
                        echo "<h3> No records </h3>";
                        
                    }
                    
                ?&gt;

but this only displays parent comments if that makes sense, i.e. comments with a parent_id of 0


this is really giving me trouble! grrr

#7
[eluser]pickupman[/eluser]
You will want to create a multidimensional array of your comments and their replies. Then when looping through the comments check if comments is array, and has reply comments. And echo each reply. You'll probably want to create a recursive function for this.

#8
[eluser]deczo[/eluser]
For 2 levels only the easiest way will be just to select comments and order them properly, then output them with right class.

Let's say we have such table:
Code:
mysql> select * from comments;
+----+----------+------------+
| id | parentId | content    |
+----+----------+------------+
|  1 |     NULL | comment 1  |
|  2 |     NULL | comment 2  |
|  3 |     NULL | comment 3  |
|  4 |        2 | comment 4  |
|  5 |        1 | comment 5  |
|  6 |        2 | comment 6  |
|  7 |     NULL | comment 7  |
|  8 |        3 | comment 8  |
|  9 |     NULL | comment 9  |
| 10 |     NULL | comment 10 |
| 11 |     NULL | comment 11 |
| 12 |        3 | comment 12 |
| 13 |        2 | comment 13 |
| 14 |        1 | comment 14 |
| 15 |        3 | comment 15 |
+----+----------+------------+
15 rows in set (0.00 sec)

Then we have to use such select statements to order comments (I'm pretty sure this would be the fastest query, correct me someone if I'm wrong):
Code:
mysql>  SELECT id, (CASE WHEN parentId IS NULL THEN id ELSE parentId END) AS parent, content FROM comments ORDER BY parent ASC, id ASC;
+----+--------+------------+
| id | parent | content    |
+----+--------+------------+
|  1 |      1 | comment 1  |
|  5 |      1 | comment 5  |
| 14 |      1 | comment 14 |
|  2 |      2 | comment 2  |
|  4 |      2 | comment 4  |
|  6 |      2 | comment 6  |
| 13 |      2 | comment 13 |
|  3 |      3 | comment 3  |
|  8 |      3 | comment 8  |
| 12 |      3 | comment 12 |
| 15 |      3 | comment 15 |
|  7 |      7 | comment 7  |
|  9 |      9 | comment 9  |
| 10 |     10 | comment 10 |
| 11 |     11 | comment 11 |
+----+--------+------------+
15 rows in set (0.00 sec)


Then all we have to do is check on the php side:
Code:
foreach ($comments as $comment)
{
  if ($comment->id == $comment->parent)
  {
    // this comment has no parent
    // output HTML, e.g. div.parent
  }else{
    // this comment is a child
    // output HTML, e.g. div.child
  }
}
assuming that $comments is the object obtained from database using the above query.

And now you may style classes .parent and .child as you wish.

Hope that helps!

EDIT: You may also let SQL do the job by a bit more complex query:
Code:
mysql> select c.id, c.parent, c.content, (CASE WHEN id=parent THEN 1 ELSE 0 END) as isParent FROM ( SELECT id, (CASE WHEN parentId IS NULL THEN id ELSE parentId END) AS parent, content FROM comments ORDER BY parent ASC, id ASC) c;
+----+--------+------------+----------+
| id | parent | content    | isParent |
+----+--------+------------+----------+
|  1 |      1 | comment 1  |        1 |
|  5 |      1 | comment 5  |        0 |
| 14 |      1 | comment 14 |        0 |
|  2 |      2 | comment 2  |        1 |
|  4 |      2 | comment 4  |        0 |
|  6 |      2 | comment 6  |        0 |
| 13 |      2 | comment 13 |        0 |
|  3 |      3 | comment 3  |        1 |
|  8 |      3 | comment 8  |        0 |
| 12 |      3 | comment 12 |        0 |
| 15 |      3 | comment 15 |        0 |
|  7 |      7 | comment 7  |        1 |
|  9 |      9 | comment 9  |        1 |
| 10 |     10 | comment 10 |        1 |
| 11 |     11 | comment 11 |        1 |
+----+--------+------------+----------+
15 rows in set (0.00 sec)

then with PHP just check if ($comment->isParent == 1) in the loop.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.