Count views results

Here's a short snippet howto create custom result counters with index for your views in Drupal 5, Drupal 6 and Drupal 7.

Drupal 7 see here.

An example of this code could result:

Showing 21 - 30 of 46 results.

A very common usage, but not available out-of-the-box in Views 1 or 2. You will need to theme a bit using some code snippets. Here's how you can do it:

Drupal 5 - Views 1

In Drupal 5, you can simply copy-paste this php code into the header part of your View.

// here are the variables we will need
global $pager_page_array, $pager_total_items, $pager_total, $current_view;

if ($pager_total[0] == 1) {
// if there is only one page, do this:
echo "Showing " . $pager_total_items[0] . " results.";
} else {
// if there are more pages, we will need to take the current page
// and multiply it with the number of results per page
// add one, because the first page is zero.
$from = 1 + ($pager_page_array[0] * $current_view->pager_limit);
// add one page to get the last result in this particular page
$to = (1 + $pager_page_array[0]) * $current_view->pager_limit;
// check if the last page has less items than the amount allowed on each page
if ($to > $pager_total_items[0]) {
$to = $pager_total_items[0];
echo "Showing " . $from . " - " . $to . " of " . $pager_total_items[0] . " results.";

Drupal 6 - Views 2

In Drupal 6 and Views 2, the easiest way (in my opinion) is to create a custom template for your view and add the necessary code beneath. Unlike the above example for Drupal 5, this code will *not* work in the header of the view if you embed it using the Views 2 user interface.

update: actually it is possible to put a similar kind of code in the Header area of the view. However, some necessary variables are only populated when the pager is turned on. See this thread for more info. You can still do this in the theming layer with the code beneath.

So either create something like views-view.tpl.php (if you want to override all views) or views-view--[viewname].tpl.php for one view. You can find the required filename and template code by clicking on the "Theme: information" link in your admin interface in Views 2.

Now, you can embed this code anywhere you like:

$from = ($view->pager['current_page'] * $view->pager['items_per_page']) + 1;
$to = $from + count($view->result) - 1;
$total = $view->total_rows;
if ($total <= $to ) {
// no need to show where we are if everything fits on the first page
echo "Showing " . $total . " results.";
} else {
echo "Showing " . $from . " - " . $to . " of " . $total . " results.";
Custom pager or fields in views
Here are some related modules which might come in handy:

Getting Drupal ready for mobile

Read More »


This is the company blog of
Drupal specialist

We are located in Breda (Netherlands) and build websites using Drupal. More about us.

Content on this blog is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Netherlands License.

Creative Commons License

Recent Comments