blog-banner

How To Display Views Result Count in Drupal 7

  • Drupal 7
  • Drupal Planet
  • Templates
  • Theme
  • Views

Drupal 7 Node Template

 

Views one of the prevailing modules in Drupal. It has the competence to dwindle the manual work of pulling the data from the Drupal table and formatting them to meet our needs. Most of the developers might have known about the filter and the sort criterion in the views, but only a few know the exact way to display the current items and total result count in this view. Does this require acquaintance with coding? Apparently yes, but not as tedious as one would imagine. All it needs is a single-line PHP print statement in the view's template file.

Let's take an example view that was created with the intention to display the products node in a grid format. The machine's name in my view is the product.

The template file can be located within your views.

  1. Click on Structure > views > your view.
  2. Now click on Advanced > Theme Information
  3. Select the display output and copy/paste the code suggested by views module to file to one of the template suggestions
  4. I would prefer to select the file name as views-view--product--page.tpl.php.
  5. Save the file to your default themes folder. In my case it is /sites/all/themes/MyTheme
  6. Now back to your views and click on Rescan template files. Now you will notice that your template file is shown in block letters, as a sign of having your template file recognized by views module.

Once the above steps are over the custom template file will be used as the template file for the view in discussion.

How do show results and counts in a view? It's time to play with a few lines of code. Open the above-created file in a text editor of your choice. To display the views result count add the following snippet. This result can be placed at the top of the view's page. Hence I would prefer to place the custom code right above the $title in the template file.

  1. <div class="sample">
  2. <?php echo t('Showing !count of !total view item', array('!count' => count($view->result), '!total' => $view->total_rows)); ?>
  3. </div>

The $view-> result indicates the array of result sets. The $view->total_rows holds the count of overall rows in the view.

 

Get awesome tech content in your inbox