Posted by vstmusic on December 16, 2008 at 6:46pm
Jump to:
| Project: | Views |
| Version: | 6.x-2.7 |
| Component: | Code |
| Category: | task |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | closed (fixed) |
Issue Summary
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
#1
please, help me !
#2
Hi!
You can use this code in template of your views/block for total results of your view.
$view->total_rowsexample
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
#3
It does not work !
It seems that "total_rows" is not used by VIEWS 2 !
Other solution ?
Thanks for your help !!
#4
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 ....
#5
I don't understant how use this query with views 2 to select the total count of nodes of a view. Is it possible ?
#6
it works for me.
Do you insert it into your views block/field/whatever template?
#7
Yes, I do but it does not work.
#8
$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.
#9
- 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 !
#10
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()
#11
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 !
#12
Please, no idea ?
#13
Automatically closed -- issue fixed for 2 weeks with no activity.
#14
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
#15
i tryed the same code there in the header and it worked.
could you have a look whether $view is defined there?
#16
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
#17
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
<?php
$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
#18
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.
#19
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:
<?php// 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
#20
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.
#21
Have a look at http://drupal.org/node/534714#comment-1916806
#22
Thank you very much Nancy.
Very cool bit of code
#23
As merlinofchaos stated in #8,
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:
<?phpfunction 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.
#24
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.
#25
So the fix in #23 almost works for me, but I need to change it to:
<?phpfunction 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.
#26
#623498: Should $view->total_rows always have the count of total rows? is the follow-up to this.
#27
If you have the pager turned off you could do something like
$total_results = count($view->result);
#28
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()
<?php/**
* 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)