In a Realty website, I have set up three blocks in the home page for listing the current projects, future projects and completed projects. I have setup three different views to print the project names into each of these blocks. Each of the views print only the top 5 projects in each of the blocks, based on the status of the project.

My requirement is to also print the total number of current projects in that block. I am using block-blockname.tpl.php to print the block as well as the view result count.

Following the solutions suggested in the post - http://drupal.org/node/131031, I have tried using "$view = views_get_current_view();" but it did not work. Further down in the same post, the following snippet was suggested by brian_c, which worked:

<?php

// load view object (has no results yet)
$view = views_get_view( 'name_of_view' );

// 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 );

?>

It prints 10 with the view name that i am using for current projects. But there are 27 current projects and the view correctly returns all the 27 projects when viewed in the node. I have tried doing a print_r($view->result) in the home page block, it prints the project names with other values and prints only 10 of them.

Am I using the right method to print the count of view query?
Thanks in advance.

Comments

crabsoul’s picture

did you try below code:

print $GLOBALS['current_view']->total_rows;
echopx’s picture

Yes. I have tried this method too. But it does not print anything. Why it might be printing only 10 with the $view = views_get_view( 'view_name' ) ? Is there any other approach I can take?

Thanks Rashmi.
Sridhar

asak’s picture

So bizzare - i'm getting only 10 results as well when using count() - but get all 12 when printing the view!

Any ideas...?

crabsoul’s picture

Hi,

I've created a view named as "test". It has 8 total records and i set "Items per page"=2. When i tried to run below code

$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;
 

it print the exact total nodes available and that is 8. So i think your code is perfect, please check other settings.

Thanks
Rashmi

Eliza’s picture

Is there any way to number raws of the view in spite of using pager? I use full node style view without fields. What must be the code for views-view-row-node--myview.tpl.php to get ordinal number at the top of each node in the view (number not per page, but in the whole view)?

crabsoul’s picture

Above code returns total number of resulted rows, no matter pager has been set or not. so you can use that in tpl file to print total number of rows.

Eliza’s picture

I see, but I don`t need total number, I need number by order of each node in the view (like ordered list without pager).

wayfarer_boy’s picture

I'm was having the same problem. I found that this code works:

$view = views_get_view($viewname);
$view->set_display($display_id);
$view->set_arguments($args);
$view->execute_display($display_id, $args);
print count($view->result).' rows';

I think it might be to do with $view->execute(); returning results for the the default view, and if the default view is set to display 10 items your count will always be 10, regardless. Specify the display id (eg. 'page_1') where you've set unlimited items to display, and you'll get the total number of rows for your view.

At least, that's my interpretation!