Number of results in a Drupal View

Find the number of results in a view. Add this snippet to the header or footer of the view, depending on where you want the number of results to be displayed.

From: http://drupal.org/node/131031

  1. $view = views_get_current_view();
  2.  
  3. // execute view query
  4. $view->execute();
  5.  
  6. // results are now stored as an array in $view->result
  7. $count = count( $view->result );
  8.  
  9. print "Showing <b>$count</b> results";

If you liked this, you'll love my book, Master Drupal Module Development.

"..the must have drupal developers book"


Feeling stuck with Drupal 8 module dev?

Get the free 7 lesson course that will help you get started today without feeling overwhelmed.

  • Create Drupal modules with just a few commands using the Drupal Console
  • Create custom pages
  • Create custom blocks
  • Create admin forms
  • Demystify routers and controllers
  • Bonus material

Find out more


Comments

Your code really helped me alot....it saved much of my time...

Thank you very much....

Thanks for this!

Thanks for the code, how do i display count of a view in a place other than the view..im guessing its like

$view = my_view_name; ?

Blair Wadman's picture

You could use views_get_view().

So:
$view = views_get_view('my_view_name')

More info here http://api.drupal.org/api/views/views.module/function/views_get_view/5

This works well for me and deals with the pager issues. Put this function in your custom module, rename / format as needed, and call it from your views-view--view_name_goes_here.tpl.php files.

function get_view_rowcount(){

$view = views_get_current_view();
$page_total = count($view-&gt;result);
if(isset($view-&gt;total_rows)){
return "<strong>Displaying " . $page_total . " of " . $view-&gt;total_rows . " total rows.</strong>";
} else {
return "<strong>Displaying " . $page_total . " of " . $page_total . " total rows.</strong>";
}
}

and it saved me much time...
I'm interested in your lesson book, but for many reasons (several modules has not yet porting on D7) i'm now developing on D6 version. Do you think it can be the same way useful for learn module building in D6?
Thanks for you answer.

Add new comment