Drupal – Add View Result Count

Sometimes we may want to show the number of records in a view. I found a very good snippet which serve this purpose perfectly and you can add it to either view header or footer.

<?php
  global $pager_page_array, $pager_total_items, $pager_total;
  $my_view = views_get_current_view();
  $items_per_page = $my_view->pager['items_per_page'];
  if ($pager_total[0] == 1) {
    echo t('Showing <b>!pager_total_items</b> results', array('!pager_total_items' => $pager_total_items[0]));
  } else {
    $start = 1 + ($pager_page_array[0] * $items_per_page);
    $end = (1 + $pager_page_array[0]) * $items_per_page;
    if ($end > $pager_total_items[0]) {
      $end = $pager_total_items[0];
      echo t('Showing !start-!end of <b>!pager_total_items</b> results', array('!start' => $start, '!end' => $end, '!pager_total_items' => $pager_total_items[0]));
    }
  }
?>

 

Here is the output view.

 

Done =)

Reference: Count view query results with footer PHP snippet

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.