Note that there are some explanatory texts on larger screens.

plurals
  1. PONested comment system mysql ordering
    primarykey
    data
    text
    <p>Ok, I realize there are many ways to accomplish comments. The way I have chosen is a single table set up like this.</p> <pre><code>id comment date time orig_comment 1 Hello 03-01-2013 10:10:10 0 2 Hello 03-02-2013 10:10:10 0 3 Hello 03-03-2013 10:10:10 1 </code></pre> <p>So to clearify there is a first level and then users can reply to that comment(these are the only two levels). I give each unique incrementing id, and specify orig_comment. If orig_comment is "0" then it is a base level comment, if it is nested then orig_comment will be the id of the original comment. Simple enough. So I need the comments to be ordered of course. Here is how I do this as of now.</p> <pre><code>mysql_query("SELECT Comments.* FROM Comments ORDER BY IF(Comments.orig_comment = 0, Comments.id, Comments.orig_comment)DESC,Comments.id ASC") </code></pre> <p>This works to get the newest comments retrieved first(which is what I want), but when it comes to nested comments it orders them by oldest first(not what I need). I need to order primary comments from newest to oldest, and any replies should be ordered also from newest to oldest. I have tried without success to modify my query to do this but cannot figure it out.</p> <p>As a side question does this system make sense from a usabality standpoint? Do you think ordering nested comments that way would be confusing? I wanted to do it this way because my reply button is located on the base comment, and once clicked adds a textarea right below the base comment. I just thought it would be easier to see your post if it too came right below the base comment instead of being thrown all the way to the bottom of the nested replies. Any thoughts on this?</p> <p>Also want to address another question I have using this to page comments. I can use a simple limit to get only the x number of comments like this:</p> <pre><code>mysql_query("SELECT Comments.* FROM Comments ORDER BY IF(Comments.orig_comment = 0, Comments.id, Comments.orig_comment)DESC,Comments.id ASC LIMIT 0, $page") </code></pre> <p>This method obviously does not pay attention to the number of replies to each main comment. So I end up cutting off replies to the very last main comment. I would like to enforce the limit only when orig_comment='0', so that no matter how many replies there are to a comment it will show all of them. I tried</p> <pre><code>mysql_query("SELECT Comments.* FROM Comments ORDER BY IF(Comments.orig_comment = 0, Comments.id, Comments.orig_comment)DESC,Comments.id ASC LIMIT 0, SELECT COUNT(id)FROM Comments WHERE orig_comment='0' LIMIT $page") </code></pre> <p>This throws a syntax error though.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload