13

I have a view in Drupal that filters my content. It brings back 7 rows. All I want to return is the number or results returned(7). Is this possible?

I tried using the View result counter but it returns a number for each results

1 2 3 4 5 6 7

I just need the 7 part.

So in SQL I would do a select count(*)

Owen Blacker
  • 4,117
  • 2
  • 33
  • 70
Linda
  • 2,227
  • 5
  • 30
  • 40
  • 2
    possible duplicate of [Count number of results in a View](http://stackoverflow.com/questions/2952901/count-number-of-results-in-a-view) – googletorp Jun 04 '10 at 12:36
  • Possible duplicate of [Count number of results in a View](https://stackoverflow.com/questions/2952901/count-number-of-results-in-a-view) – apaderno May 14 '19 at 09:31

6 Answers6

19

what you can do is to activate php for the views header/footer and add the following snippet to it:

<?php
  $view = views_get_current_view();
  print $view->total_rows; 
?>

This will print the total number of rows.

If you need the result as a field, you could use the "Views custom field" module, add a php field and run the same snippet.

Regards

Mike

mikewink
  • 529
  • 3
  • 8
  • 1
    Really not a very good idea to put PHP in the header and footer of a view! It's impossible to version this code, it's easy to forget it's there, it has no error checking on it, and more seriously, anyone with permission to edit the view has the permission to destroy your site by issuing a command that will delete the database. – Chris Cohen Sep 05 '12 at 10:43
  • 2
    Override the view's footer/header in code in a module or in a template: I think something like this: http://knackforge.com/blog/sabareesh/how-display-views-result-count-drupal-7 – Meetai.com Oct 09 '12 at 00:11
  • Completely agree with Chris - this is very bad practice, use Air's solution instead. – Felix Eve Dec 02 '13 at 10:58
  • If you featurize your views, you get versioning through your features. – diamondsea Mar 09 '18 at 20:59
5

If you want to get the count outside the view you can use this

$view = views_get_view('MY_VIEW_NAME');

$view->set_display('MY_DISPLAY'); // like 'block_1'    

$view->render();   

print sizeof($view->result);

Note : Do not use this within a view. It will be an overhead. If you are using it within view check the other answers.

Gokul N K
  • 2,428
  • 2
  • 32
  • 40
3

If using views_get_view in Views 3, you can use this snippet:

    $view = views_get_view('MY_VIEW');
    $view->set_display('MY_DISPLAY');
    // Execute first
    $result = $view->preview('MY_DISPLAY');
    // Output result only if rows > 0
    if (count($view->result) > 0) {
      print $result;
    }
dotoree
  • 2,983
  • 1
  • 24
  • 29
2

With Views 3 you may need to do

$view->get_total_rows = TRUE;
$total_items = $view->query->pager->get_total_items();
1

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->result);
 if(isset($view->total_rows)){
   return "<strong>Displaying " . $page_total . " of " . $view->total_rows . " total rows.</strong>";
 } else {
  return "<strong>Displaying " . $page_total . " of " . $page_total . " total rows.</strong>";
 }
}
Sibiraj PR
  • 1,481
  • 1
  • 10
  • 25
Darrell Duane
  • 3,874
  • 1
  • 18
  • 9
0

with drupal 7 - Under the pager options you have the option to Expose items per page When checked, users can determine how many items per page show in a view

Diana
  • 234
  • 1
  • 4
  • 15