1 comment.module comment_get_display_ordinal($cid, $node_type)

Gets the display ordinal for a comment, starting from 0.

Count the number of comments which appear before the comment we want to display, taking into account display settings and threading.


int $cid: The comment ID.

string $node_type: The node type of the comment's parent.

Return value

The display ordinal for the comment.:

See also



core/modules/comment/comment.module, line 1660
Enables users to comment on published content.


function comment_get_display_ordinal($cid, $node_type) {
  // Count how many comments (c1) are before $cid (c2) in display order. This is
  // the 0-based display ordinal.
  $query = db_select('comment', 'c1');
  $query->innerJoin('comment', 'c2', 'c2.nid = c1.nid');
  $query->addExpression('COUNT(*)', 'count');
  $query->condition('c2.cid', $cid);
  if (!user_access('administer comments')) {
    $query->condition('c1.status', COMMENT_PUBLISHED);
  $type = node_type_get_type($node_type);
  $mode = $type->settings['comment_mode'];

  if ($mode == COMMENT_MODE_FLAT) {
    // For flat comments, cid is used for ordering comments due to
    // unpredictable behavior with timestamp, so we make the same assumption
    // here.
    $query->condition('c1.cid', $cid, '<');
  else {
    // For threaded comments, the c.thread column is used for ordering. We can
    // use the sorting code for comparison, but must remove the trailing slash.
    // See comment_view_multiple().
    $query->where('SUBSTRING(c1.thread, 1, (LENGTH(c1.thread) -1)) < SUBSTRING(c2.thread, 1, (LENGTH(c2.thread) -1))');

  return $query->execute()->fetchField();