Closed (fixed)
Project:
Views (for Drupal 7)
Version:
6.x-2.7
Component:
Code
Priority:
Normal
Category:
Task
Assigned:
Unassigned
Reporter:
Created:
16 Dec 2008 at 18:46 UTC
Updated:
20 Jul 2016 at 14:13 UTC
Jump to comment: Most recent
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
Comment #1
vstmusic commentedplease, help me !
Comment #2
igorik commentedHi!
You can use this code in template of your views/block for total results of your view.
example
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
Comment #3
vstmusic commentedIt does not work !
It seems that "total_rows" is not used by VIEWS 2 !
Other solution ?
Thanks for your help !!
Comment #4
computer_jin commentedU 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 ....
Comment #5
vstmusic commentedI don't understant how use this query with views 2 to select the total count of nodes of a view. Is it possible ?
Comment #6
igorik commentedit works for me.
Do you insert it into your views block/field/whatever template?
Comment #7
vstmusic commentedYes, I do but it does not work.
Comment #8
merlinofchaos commented$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.
Comment #9
vstmusic commented- 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 !
Comment #10
merlinofchaos commentedIf 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()
Comment #11
vstmusic commentedIt 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 !
Comment #12
vstmusic commentedPlease, no idea ?
Comment #14
davedg629 commentedI 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":
I get nothing for an output. What am I doing wrong? Thanks for any help.
Dave
Comment #15
dawehneri tryed the same code there in the header and it worked.
could you have a look whether $view is defined there?
Comment #16
nrackleff commentedI 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
Comment #17
nrackleff commentedI 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
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
Comment #18
markDrupal commentedThere 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.
Comment #19
seutje commentedwhat 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:
tested on Views 6.x-2.6, works both in header and footer
Comment #20
vstmusic commentedA 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.
Comment #21
dawehnerHave a look at http://drupal.org/node/534714#comment-1916806
Comment #22
dadderley commentedThank you very much Nancy.
Very cool bit of code
Comment #23
tom_o_t commentedAs 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:
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.
Comment #24
rjbrown99 commentedThanks 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:
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.
Comment #25
marcp commentedSo the fix in #23 almost works for me, but I need to change it to:
because the code in execute() checks items_per_page before it lets you through to the get_total_rows check:
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.
Comment #26
marcp commented#623498: Should $view->total_rows always have the count of total rows? is the follow-up to this.
Comment #27
sherakama commentedIf you have the pager turned off you could do something like
$total_results = count($view->result);
Comment #28
dman commentedFor 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()
(updated code to resolve a further issue from testing)
Comment #29
darrellduane commentedThis 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.
Comment #30
thelionkingraja commentedHello,
There is a field "Global: Result summary" (D7) under Header of view. you can display the count using this.
Thanks, Raja
Comment #31
zet commentedD6 too has a "Global: View result counter" field
Comment #32
tryitonce commented... 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 ...
Comment #33
Jenechka commentedSuggestions from the above work only if pager is enabled.
Otherwise use:
Comment #34
code-brighton commentedI think this works if you DON'T have a pager
Comment #35
eigentor commentedI can confirm the use of the "global result summary" described in #30. This works with and without showing a pager.
Comment #36
edop commentedLike #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
Comment #37
Tharick commented#30 is working cool, thanks :)
Comment #38
xPrzybyLx commentedYou can use this snippet :
Comment #39
uzmaabdulrasheed1@gmail.com commentedHere is the solution of your problem: https://www.drupal.org/project/views_aggregator