Hi folks,

I have a View overriding the User profiles and I also embed different views in user-page.tpl.php to produce an informative profile. Those views have the UID as an argument

Right now, this profile shows the following view blocks, with respective pages linked to:

My Data (simple CCK fields belonging to the profile).
My Articles (A CCK-enabled content type).
My Photos (A CCK-enabled content type).
My Bookmarks (Flag)
My Favorites (Flag).

To this profile page I want to add something like a header, rendering the total counts of every view, so-to-speak a statistic. In this setting it should output something like:

User (identified by UID) created 5 articles.
User created 3 photos.
User has 10 bookmarks.
User has 22 favorites.

Does anyone know how to achieve this? I looked around, but could not find any solution. I thought it would be rather trivial, given that I already have thoses embedded views.

I would think there must be some template function, where I can specify a view (it must be the page view since the result of the blocks here are limited to 5, hence a more link to the page) and the corresponding argument and get the total row count to display it.

Or maybe there is another alternative to this?

Thank you in advance.


theusualsuspect’s picture

Please, please.. I need help for this... Anybody there...?

niner94949’s picture

I'm looking to do the same thing, but have not been able to find a solution either.

Dinis’s picture

This is something I would like to do too, not sure if views has a "sum" function in any of it's outputs though.

I suspect this will need a module to extend the funcionality of Views.

dawehner’s picture

I suggest you to look at this code.

Id has everything you need

    $count = is_numeric($this->options['counter_start']) ? $this->options['counter_start'] - 1 : 0;
    $pager = $this->view->pager;
    // Get the base count of the pager.
    if ($pager['use_pager']) {
      $count += ($pager['items_per_page'] * $pager['current_page']) + $pager['offset'];

You just have to change take the $count at the end.

theusualsuspect’s picture

@dereine Thank you for your code. But could you provide us with a little more information. Where does this block go? In the Global custom text for instance?

vstmusic’s picture

When I put your code in a block or in the header oh a view -> white screen. Did you test it ?

thanks for your help

dawehner’s picture

I just copied the code from the counter field handler.

bfr’s picture

Why not just use views calc module for this?

merlinofchaos’s picture

Status: Active » Fixed

Yeah, views calc will do this.

Status: Fixed » Closed (fixed)

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

NoRandom’s picture

I put this code in the footer or header of my views:

    $view = views_get_current_view();
    print '<div class="view-counter">Item number: ' . $view->total_rows . '</div>';
houdelou’s picture

Here is a function that returns an array of view results :

views_get_view_result($viewname, $display_id, $args);


count(views_get_view_result($viewname, $display_id, $args));

would do the trick.

bfr’s picture

Ofcourse, if you really want it to header, you can just use php and get anything you want with a custom sql query(if they dont have to be linked to results of views, but you just want totals of everything).

one approach out of many:

global $user;
$result = db_query("select * from {node} where uid = $user->uid");
while ($row = db_fetch_array($result)) {
print "You have written ".${"number_of_type_page"}." page posts<br>";
print "You have written ".${"number_of_type_blog"}." blog posts<br>";

Of course, the same can be achieved(and faster) by using count in the query but i like not to make very complicated queries and make the code easier to read instead, if there are no performance issues.

kevinsiji’s picture

This code really helped. It brings the total count of a views result set, pager settings are not taken into consideration.

kevinsiji’s picture

This code gives only the total number of records in the current page of a paged view. So effectively it is the 'items to display' count.

bfr’s picture

If you mean my code from @13, it actually does not even take the view into consideration, it just prints the total number of posts the user has made. You can use it outside views also.

deng17’s picture

Can you modify that code so that it would count the number of instances of CCK fields from the current view?

For example: field_color = Red , it will count all nodes in the current view that have the same value

TheodorosPloumis’s picture

The code from #11 worked perfect for me too (D 6.19, Views 3.x). Thanks a lot.

gregoiresan’s picture

I'm not sure you need this code in Views 3.x

dmetzcher’s picture

Comment #11 was exactly what I needed. Simple, clean and it works.

ranavaibhav’s picture

Following code works for me with Drupal 6.19, Views 6.3 alpha-3
I use this code under views header section.

<?php print count(views_get_current_view()->result); ?>
gregoiresan’s picture

With user stats, it is possible to have the total number of created nodes by node type for a user.
With flags, you can have the number of favorite nodes, by node types

so, you just need to
- create a user view with the proper relationships, then
- make a field by node type with user stats and flags,

Voilà, you have your stats.

It is not lighter than the code given above, but probably more flexible and easier to user for non-coders people.

diricia’s picture

Put the following code in the header of your view. Make sure the input format is php.

$view = views_get_current_view();
print 'Total rows: ' .  $view->total_rows;

This will also take into account any exposed filters that where set. :)

aac’s picture

This one really helped me too.

husumiao’s picture

Thanks for NoRandom.
I got the idea.

candelas’s picture

@NoRandom thanks! simple and fast!
works with views php module :)

Ayesh’s picture

Not to post spam links actually, but if you are looking for a way to show the number of results of a View anywhere else from the View, see (my own) http://ayesh.me/content/all-about-counting-views-results .
Non of the above posts gives a code to show results count of a View from elsewhere in Drupla site, and sometimes you get wrong results due to pager setting.

ankitchauhan’s picture

Status: Closed (fixed) » Active

The code that is written above work but only for default display.

Here is a module named views record count for counting views query result. This module provides the functionality to customize the message display region (header/footer) and also work with overridden values in multiple display in a single view. But for that you have to enable the page in your views display.

Tom Kraft’s picture

The code snippets in #11 and #23 worked perfect in the header PHP code of my views. It showed the total number of node results. That was until I upgraded Views to version 6.x-2.16. Now the count is display a total multiplied by 3. Is anyone else experiencing a similar result? I guess is there a PHP code work-around that will divide the total by 3 to get the true number? This is the code I'm using from above:

    $view = views_get_current_view();
    print '<div class="view-counter">Results: ' . $view->total_rows . ' Pages</div>';

Any help is appreciated. Thanks.

bfr’s picture

Well, i guess you could do:

    $view = views_get_current_view();
    print '<div class="view-counter">Results: ' . $view->total_rows / 3 . ' Pages</div>';

But instead i suggest you do dsm($view) or print_r($view) and find out what is the cause of this. The number you are looking for could have moved to some other property of the $view object. Maybe it's counting the total number of fields or something like that now? Or maybe you have pager enabled and you are not taking into account the results on other pages?

Tom Kraft’s picture

Thanks, I'll try the latter. Dividing the number by 3 works in some cases but other cases it puts .33333 after the number...

Kars-T’s picture

Status: Active » Fixed


I am closing this issue to clean up the issue queue. Feel free to reopen the issue if there is new information and the problem still resides. If not please make sure you close your issues that you don't need any more.

Maybe you can get support from the local user group. Please take a look at this list at groups.drupal.org.

Status: Fixed » Closed (fixed)

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

Alex72RM’s picture

Version: 6.x-2.6 » 8.x-3.x-dev
Category: Support request » Task
Issue summary: View changes
Status: Closed (fixed) » Active

Hi there,

I try to reopen the issue to ask about the Drupal 8 porting of solutions taken into account in this thread...


Lendude’s picture

Project: Views » Drupal core
Version: 8.x-3.x-dev » 8.1.x-dev
Component: Miscellaneous » views.module
Status: Active » Closed (works as designed)

You can print all this stuff in the header/footer using the 'Global: Result summary' handler. If that doesn't suffice, please open a feature request issue with what you feel is missing there.

Moving to the right queue.