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

Avatar-eric-london
Created by Eric.London on 2009-03-20
Tags:
New Comment
 
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.

Comments

 
  • correct me if I'm wrong, but
    Created by Anonymous on 2009-06-13
    correct me if I'm wrong, but shouldn't the $start assignment look more like:

    $start = ($itemsPerPage * $currentPage) - ($itemsPerPage-1);

    As it stands, the $start assumes 10 items per page, so fetching the items per page doesn't net you anything.
    • Eric, thank you so much!
      Created by Anonymous on 2010-05-04
      This line made my day: $view->get_total_rows = true;
      I did not know if this is not set, that I cannot get the $view->total_rows value.

      Cheers

      Chris
  • what if
    Created by Anonymous on 2010-06-18
    what if your view requires an argument?
  • isn't it executing the view twice?
    Created by Anonymous on 2010-10-01
    yeah I mean views already execute once to get records and all that but than in its rendering theme we are going to execute it again using $view->execute(); ? I am little concerned about it as my view is already heavy and executing twice is going to make the page real slow.

    Shouldn't we use &$view as argument to this function? Any idea?
  • Aja
    Created by Anonymous on 2011-04-07
    Hi Eric, and thaks for the graeat post, just one question: how do I make it work when using Ajax ono view?