Hi,

I want to display the number of results of such view in a html text.

Must I create a php snippet code ? What code must I create to display the total number of results of a view ?

Tans for your help !

Cédric

Comments

vstmusic’s picture

please, help me !

igorik’s picture

Hi!

You can use this code in template of your views/block for total results of your view.

$view->total_rows

example

print 'Total results(nodes): ' . $view->total_rows;

I do it this way too. because I don't know if it could be done right inside editing view page, I read all issues but I found no info about it.

Igorik
http://www.somvprahe.sk

vstmusic’s picture

It does not work !

It seems that "total_rows" is not used by VIEWS 2 !

Other solution ?

Thanks for your help !!

computer_jin’s picture

U can use count query to count the result from the query
Exp:

$query = "Select count(*) from table ' ;

it will return you number of rows in this table ....

vstmusic’s picture

I don't understant how use this query with views 2 to select the total count of nodes of a view. Is it possible ?

igorik’s picture

it works for me.
Do you insert it into your views block/field/whatever template?

vstmusic’s picture

Yes, I do but it does not work.

merlinofchaos’s picture

$view->total_rows is only filled in if the pager is turned on, which is probably why it is not working for you. If the pager is off, the query is not run.

vstmusic’s picture

- Pager is turned on.

- I use php filter in the footer of my view:

<?
print 'Total results(nodes): ' . $view->total_rows;
?>

I don't understand why it does not work.

Is there another solution to count the total number of nodes in a view ? (I must do that for many views).

Thanks for your help !

merlinofchaos’s picture

Status: Active » Fixed

If you're using the PHP filter, then $view is undefined, because Views has no way to inject variables into that eval. You need to do $view = views_get_current_view()

vstmusic’s picture

It run if I add "$view = views_get_current_view()" ! Thanks !

Another question :

Is it possible to use this counter php snippet on an other page of my website ? I want to use the total count of a view named "x" on my front page. Is it possible. How can I do that (if the view is not display on the front page)?

Thanks for your help !

vstmusic’s picture

Please, no idea ?

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

davedg629’s picture

Status: Closed (fixed) » Active

I have tried the following to get this to work in D6, views 2 table view:

- Turned Pager ON in view.
- Placed the following code in the header of the view and selected the "PHP Filter":

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

I get nothing for an output. What am I doing wrong? Thanks for any help.

Dave

dawehner’s picture

i tryed the same code there in the header and it worked.

could you have a look whether $view is defined there?

nrackleff’s picture

I am struggling with this too. I am building a template for a specific node type. I want to add 3 tabs to that page and display a view block on each of the tabs. The problem is that I don't want to display the tab at all if it isn't going to have any content. I've tried the following in my tpl file and it doesn't give me a count. It doesn't print a zero or any number at all.

$view = views_get_view('product_technotes');
print 'Total results(nodes): ' . $view->total_rows;

Then, if the count is greater than zero, I will create the HTML for the tab and use views_embed_view('product_technotes', 'block_1');

I do have the mini pager turned on the the view even though I won't be displaying a pager in this block view.

Any ideas how to get this to work?

Thanks for your help. -Nancy

nrackleff’s picture

I did a bit of searching in the forum and found the answer to my question so I'll post if here in case anyone else needs it too. This will get the count of items returned in a view in Drupal 6.

http://drupal.org/node/508130#comment-1771212


$view = views_get_view( 'test' );

// set arguments (if needed, otherwise omit this line)
$view->set_arguments( array( 1, 2, 3 ) );

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

// results are now stored as an array in $view->result
$count = count( $view->result );
//print_r ($view);
print $count;

I guess in my case I could also have just checked to see if the view was not empty with if (!empty($view)) since I just needed to know if it had items as opposed to knowing the exact number of items.

Anyhow, there it is if anyone needs it. -Nancy

markDrupal’s picture

There is this module ( http://drupal.org/project/views_calc ) as well that allows you to add a "COUNT(*)" field to your view. It might be better (faster) than putting PHP snippits in the header.

seutje’s picture

what the hell, are you ppl serious?

ur reloading a view that's already loaded, ur also making that view execute, resulting in the same thing as having the same view on a page twice (thank god it uses statics)

also, avoid redefining the $view variable in the header/footer as views uses the same namespace and u'll most likely wreck the active view (same for $handler and $argument and such, prefix them with "my_awesome_" or something silly to avoid scoping issues)

instead try this:

// intentionally using a silly var name to avoid scoping issues
$my_view = views_get_current_view();
// again, avoiding scoping issues
$my_total = $my_view->total_rows;
// too lazy to do proper t('') with placeholder
print '<p>' . $my_total . ' records matched criteria.</p>';

tested on Views 6.x-2.6, works both in header and footer

vstmusic’s picture

A similar problem:

How display the number of results (total count) of a view A IN a table of a view B ?

View A is displayed on every node of a content type and it have an argument depending of the node ID.

View B displays the title of each node (disaplayed in view A) and I want it displays too the number of results of view A for each node.

Is it possible ? With php code inserted in view B ? What code ?

Do you understand my problem ?

Thanks for your help.

dawehner’s picture

dadderley’s picture

Thank you very much Nancy.
Very cool bit of code

tom_o_t’s picture

Status: Active » Closed (fixed)

As merlinofchaos stated in #8,

$view->total_rows is only filled in if the pager is turned on

If you need to add the total_rows count to the views object there's a flag you can add to the view in a custom module:

function mycustommodule_views_pre_execute(&$view) {
  $view->get_total_rows = TRUE;
}

The code above will add the total row count to ALL views, which will add a little overhead to your site, so you may want to check the $view->name before setting get_total_rows to be TRUE.

You can then access the $view object in a views template file (e.g. views-view.tpl.php) and use this count as necessary. In my case I'm adding the row count to the 'more' link in all views.

Thanks to merlinofchaos for helping me figure this out.

rjbrown99’s picture

Thanks for this thread. I had a need to create a block that only displays a counter if the view comes back as empty. Basically I'm trying to nudge users to complete CCK fields, and if the fields aren't complete I want a block to tell them about it so they go and complete them. It's a little different from content_complete module because it isn't dealing with profiles - these are for nodes of content (in this case, pictures and descriptions of them.)

Here's what I did.

1) Created a block, users can't control whether they see it, and show on all pages except the view page itself (which edits the nodes using the editablefields module)

2) Here's the PHP code for the block:

<?php
// The name of the view to load
$myviewname = 'view_postupload';
// Get the view
$myview_tocount = views_get_view($myviewname);
// Make sure we get the total row count before executing the view
$myview_tocount->get_total_rows = TRUE;
// Execute the view
$myview_tocount->execute();
// Grab the counter
$myview_total = $myview_tocount->total_rows;
// Show the counter if there are items
if ($myview_total > 0) {
  $noticeoutput = format_plural($myview_total, 
    'You have 1 photo that requires editing.',
    'You have @count photos that require editing.');
  $linkoutput .=  l(t('Click here to edit'), 'get/postupload', array('attributes' => array('class' => 'uploadincomplete')));
  print $noticeoutput;
  print $linkoutput;
}
?>

If the view comes back with anything, it shows the users the block telling them to go to the view to finish editing their fields. I don't think I can use views_get_current_view because the block can (and will) show up on many places on the site.

Perhaps there is a more efficient way of doing this. If so, feel free to chime in. Otherwise I wanted to post this as an example in case others need it in the future. Thanks to all.

marcp’s picture

Version: 6.x-2.1 » 6.x-2.7

So the fix in #23 almost works for me, but I need to change it to:

<?php
function mycustommodule_views_pre_execute(&$view) {
  $view->get_total_rows = TRUE;
  $view->pager['items_per_page'] = 999999;
}
?>

because the code in execute() checks items_per_page before it lets you through to the get_total_rows check:

        if (!empty($this->pager['items_per_page'])) {
          // We no longer use pager_query() here because pager_query() does not
          // support an offset. This is fine as we don't actually need pager
          // query; we've already been doing most of what it does, and we
          // just need to do a little more playing with globals.
          if (!empty($this->pager['use_pager']) || !empty($this->get_total_rows)) {
            $this->total_rows = db_result(db_query($count_query, $args)) - $this->pager['offset'];
          }
// ...

        }

I'm not confident in this approach, though, so I'll open another issue to request the ability to grab the number of rows in a non-paged view.

marcp’s picture

sherakama’s picture

If you have the pager turned off you could do something like

$total_results = count($view->result);

dman’s picture

For any searchers who find this thread -

As I'm avoiding enabling php format, I (after lots of trial and error) found how to do it in my own module. (Views2)

I filter to make sure I'm selecting one of my views ('staffsearch_internals') and add this to my module ('staffsearch_display')
so this is the desired outcome, thanks to HOOK_views_pre_render()

/**
 * HOOK_views_pre_render()
 * 
 * When rendering one of our own views, insert a count into the footer.
 */
function staffsearch_display_views_pre_render(&$view) {
  if ($view->name != 'staffsearch_internals') {
    return;
  }
  $view->style_plugin->display->handler->options['footer'] = count($view->result) . " items";
  $view->style_plugin->display->handler->options['defaults']['footer'] = FALSE;
}

(updated code to resolve a further issue from testing)

darrellduane’s picture

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>";
  }
}
thelionkingraja’s picture

Hello,

There is a field "Global: Result summary" (D7) under Header of view. you can display the count using this.

Thanks, Raja

zet’s picture

D6 too has a "Global: View result counter" field

tryitonce’s picture

... zet, how would you add this in D6 to the header?
If you add "Global: View result counter" field - these would show up in each row ... not at the top in/near the header ...

Jenechka’s picture

Suggestions from the above work only if pager is enabled.
Otherwise use:

$view->get_total_rows = TRUE;
$total_items = $view->query->pager->get_total_items();
code-brighton’s picture

Issue summary: View changes

I think this works if you DON'T have a pager

count(views_get_view_result('your_view', 'block_1'))
eigentor’s picture

I can confirm the use of the "global result summary" described in #30. This works with and without showing a pager.

edop’s picture

Like #30 said works fine, no coding required.
A little more explanation:
In the View edit screen, add a 'Footer' or a 'Header' (middle column of the settings) then choose "Global: Result summary". You can then choose any of these tokens:
@start -- the initial record number in the set
@end -- the last record number in the set
@total -- the total records in the set
@name -- the human-readable name of the view
@per_page -- the number of items per page
@current_page -- the current page number
@current_record_count -- the current page record count
@page_count -- the total page count

Tharick’s picture

#30 is working cool, thanks :)

xPrzybyLx’s picture

You can use this snippet :

  $view = views_get_view('view_id');
  $view->set_arguments(array($param));
  $view->execute();
  return count($view->total_rows);
uzmaabdulrasheed1@gmail.com’s picture

Here is the solution of your problem: https://www.drupal.org/project/views_aggregator