Drupal 6: Displaying the total number of results in a view and how many are being shown on the current page

When you have a view that has a lot of results, you could improve usability by showing how many total results there are and how many are being shown on the current page. Similar to this:

Displaying ### - ### of ### results

I created a function that accepts two arguments: the view name and the view's display_id (default, block, page, etc). The function loads the view, executes it, and calculates these numbers for you.

<?php
function _MYMODULE_view_totals($viewName, $display_id = 'default') {

  // load viewobject
  $view = views_get_view($viewName);

  // ensure view exists
  if (!$view) return;

  // set object property to return total rows
  $view->get_total_rows = true;

  // set display_id
  $view->set_display($display_id);

  // execute view
  $view->execute();

  // acquire data from views object and $_REQUEST
  $itemsPerPage = $view->pager['items_per_page'];
  $currentPage = $_REQUEST['page']+1;
  $total = $view->total_rows;

  // start calculation
  $start = 10*$currentPage-9;
  $end = $itemsPerPage * $currentPage;
  if ($end>$total) $end = $total;

  // return html
  return "Displaying $start - $end of $total";

}
?>

There are a few places you can insert this code. I decided to put it directing in my view, right after my exposed filters. To do that, I created a new file in my theme called: "views-view--MYVIEWNAME--page.tpl.php", and used the "Theme Information" section on the edit view screen to populate it with the default html & PHP. I searched for the code that inserts the exposed filters ($exposed), and inserted the following:

<?php
if (function_exists('_MYMODULE_view_totals')) {
  $viewTotals = _MYMODULE_view_totals('MYVIEWNAME', $display_id = 'page_1');
  if ($viewTotals) {
    print "<div class='MYTHEME_views_totals'>$viewTotals</div>";
  }
}
?>

I enclosed it in a div with a class name so I could adjust the CSS as necessary.