I've been using the above code to embed a view. How would I go about retrieving and displaying the total results returned by this view BEFORE it's embedded?
e.g. "There are x number of reviews" Embedded view...
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.
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?
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.
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)?
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!
来自 https://www.drupal.org/node/508130
How to get the total count / row count / result count of a view? E.g., get total count of all articles posted by a User (UID)
<?php
$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'];}?>
global$user;$result=db_query("select * from {node} where uid = $user->uid");while($row=db_fetch_array($result)){$type=$row[type]; ${"number_of_type_".$type}+=1;}print"You have written ".${"number_of_type_page"}." page posts";print"You have written ".${"number_of_type_blog"}." blog posts";
Comments
get, execute, and count
rodgolpe commented
Count view query result with multiple views in a page
Posted by echopx on
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:
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.
Log in or register to post comments
⋅ Categories: Drupal 6.x
Comments
did you try below
rash.jpr commented
did you try below code:
Log in or register to post comments
It does not print anything
echopx commented
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
Log in or register to post comments
So bizzare - i'm getting only
asak commented
So bizzare - i'm getting only 10 results as well when using count() - but get all 12 when printing the view!
Any ideas...?
Log in or register to post comments
Hi, I've created a view named
rash.jpr commented
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
it print the exact total nodes available and that is 8. So i think your code is perfect, please check other settings.
Thanks
Rashmi
Log in or register to post comments
ordinal number in the whole view
Eliza commented
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)?
Log in or register to post comments
Above code returns total
rash.jpr commented
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.
Log in or register to post comments
number by order
Eliza commented
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).
Log in or register to post comments
I'm was having the same
wayfarer_boy commented
I'm was having the same problem. I found that this code works:
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!
来自 https://www.drupal.org/node/508130
How to get the total count / row count / result count of a view? E.g., get total count of all articles posted by a User (UID)
<?php