Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I really like how <a href="http://drupal.org/" rel="noreferrer">Drupal</a> solves this problem. It assigns a thread id to each comment. This id starts at 1 for the first comment. If a reply is added to this comment, the id <code>1.1</code> is assigned to it. A reply to comment <code>1.1</code> is given the thread id <code>1.1.1</code>. A sibling of comment <code>1.1</code> is given the thread id <code>1.2</code>. You get the idea. Calculating these thread ids can be done easily with one query when a comment is added.</p> <p>When the thread is rendered, all of the comments that belong to the thread are fetched in a single query, sorted by the thread id. This gives you the threads in the ascending order. Furthermore, using the thread id, you can find the nesting level of each comment, and indent it accordingly.</p> <pre><code>1 1.1 1.1.1 1.2 1.2.1 </code></pre> <p>There are a few issues to sort out:</p> <ul> <li>If one component of the thread id grows to 2 digits, sorting by thread id will not produce the expected order. An easy solution is ensuring that all components of a thread id are padded by zeros to have the same width.</li> <li>Sorting by descending thread id does not produce the expected descending order.</li> </ul> <p>Drupal solves the first issue in a more complicated way using a numbering system called vancode. As for the second issue, it is solved by appending a backslash (whose ASCII code is higher than digits) to thread ids when sorting by descending order. You can find more details about this implementation by checking the source code of the <a href="http://drupalcode.org/project/drupal.git/blob/95b676c8de:/modules/comment/comment.module#l757" rel="noreferrer">comments module</a> (see the big comment before the function comment_get_thread).</p>
 

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