Views: Show "Showing X-X of X results" (page and result counter) in Drupal 7

[Update: Views 3.x has a really nifty plugin feature called 'Results summary' that you can simply add to the header or footer of your view, and use your own text with placeholders, to do everything I outline in the post below, without a line of code. Add a results summary instead of using hook_views_pre_render() or a Views PHP field.]

I needed to display a page/item counter on a view on one of my Drupal 7 sites, using Views 7.x-3.x. This counter would display at the bottom of the view, just above the pager, and needed to display the current number of results being displayed, along with the total number of results.

Views provides all this data right inside the $views object, so all I needed to do was add the following PHP snippet (including the <?php ?> delimiters) to a 'Global: PHP' textarea in my view's footer:

<?php
$from
= ($view->query->pager->current_page * $view->query->pager->options['items_per_page']) + 1;
$to = $from + count($view->result) - 1;
$total = $view->total_rows;
print
'<div class="views-result-count">';
if (
$total <= $to) {
 
// If there's no pager, just print the total.
 
print $total . ' results.';
} else {
 
// Otherwise, show "Showing X - X of XX results."
 
print 'Showing ' . $from . ' - ' . $to . ' of '. $total . ' results.';
}
print
'</div>';
?>

This prints the total number of results when there is just one page of results, and prints the range which the user is currently viewing, with the total number of results, when the user is browsing through pages using the view's pager.

You can use this code to print the pager/result counts wherever you can access the $view object (in a .tpl.php file, in the header or footer, or in a views render hook.

Edit: To make this live better in version control, and to avoid having to enable the PHP filter on your site, you can add the following code inside a custom module (which hooks into views and adds the count to the bottom of the view (switch 'attachment_after' to 'attachment_before' to show the count at the top of the view):

<?php
/**
  * Implements hook_views_pre_render().
  */
function MYMODULE_views_pre_render(&$view) {
 
dpm($view);
  if (
$view->name == 'MY_VIEW_NAME_HERE') {
   
$output = '';
   
$from = ($view->query->pager->current_page * $view->query->pager->options['items_per_page']) + 1;
   
$to = $from + count($view->result) - 1;
   
$total = $view->total_rows;
   
$output .= '<div class="views-result-count">';
    if (
$total <= $to) {
     
// If there's no pager, just print the total.
     
$output .= $total . ' results.';
    } else {
     
// Otherwise, show "Showing X - X of XX results."
     
$output .= 'Showing ' . $from . ' - ' . $to . ' of '. $total . ' results.';
    }
   
$output .= '</div>';
   
$view->attachment_after = $output;
  }
}
?>

Comments

I was looking for exactly this last night!
I'm 'guessing' you installed the views_php module for this to be able to work?
Just tested this, and it works perfectly.
Thanks Jeff!

It would be great if this was an option for the pager that is in Views. 'show results counter'
Or maybe as a separate module, or as a Views plugin or so (not sure how that works :)

The problem here is that you store code in the database (ouch), and you enabled the php text format (security risk, ouch #2!). We needed something similar (almost the same in fact), so what we did is write a plugin for Views area (header, footer and empty text are pluggable in Views 3) where we display a list of all theme functions. One of these theme functions contains the same code (almost) as yours. So we just have to choose our "Global : custom area" and select our counter function. Like this, no code in the db, no security hole opened, the code is under version control, and the output is themable :) Try it ! (sorry I can't disclose the code)

For a quick fix, though, enabling the PHP filter isn't the end of the world (especially if you don't allow it to be used for any roles, and don't let anyone log in as user 1).

There is definitely a lot to be said about putting everything in code, and never enabling the PHP filter (which I normally do on larger or more security-conscious sites), but if I did that for all of the hundreds of site's I've set up, I wouldn't have been able to do hundreds :)

But, point taken. I just wish Views 3's handbook documentation were completed (at this point, there's very little) so I didn't have to jump around so much to make sure my plugins/filters/etc. work.

Agree with you, for a quick fix where only user 1 can use it. But I promise that if the hundred sites of yours all needed the same counter, you would have been able to do them (code reusability :)
Anyway I wish I could find some time this summer to write some handbook pages for Views, so you'll know how to do it !

It seems header and footer do not have acces to $view!?

Here's how to get the $view :

$view = views_get_current_view();

Man you just saved my life, thx so much for this code !!