Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drupal return number of results in a View

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(*)

like image 596
Linda Avatar asked Jun 04 '10 12:06

Linda


3 Answers

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

like image 164
mikewink Avatar answered Sep 29 '22 09:09

mikewink


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.

like image 24
Gokul N K Avatar answered Sep 29 '22 08:09

Gokul N K


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;
    }
like image 24
dotoree Avatar answered Sep 29 '22 08:09

dotoree