By monkeybeach on
Apologies if the answer is elsewhere, all I can find are solutions for 4.x with comments that the solution doesn't work on 5.
Basically the questions in the title :-) For a view which shows a particular content-type (job) I'd like to use PHP to show in the header a count of all the nodes contained in the view.
"There are currently X jobs listed in our database"
Bonus would be if a search (exposed filter, index:search) changes the count to say how many that search exposed, but not essential as I bet this is much harder
Thanks in advance
R
Comments
Well...
A Views header can contain php code (there is an input filter field set).
Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database
NancyDru
Doesn't work for me
Could you please help? It keeps returning value of 1 (one), when there are about 150 nodes of the kind requested.
Thanks in advance
(using Drupal 5.2 - Views 5.x 1.6)
Post the code you put into
Post the code you put into the header here. Also make sure your input is set to PHP.
Exactly!
That is what I have done! The code goes to the view header with input filter set to php. Anyway, if it wasn't set to php filter the code wouldn't be executed, but still, the result returned is 1 (one) when there are about 150 nodes of the particular content type. Any ideas?
run the sql query from
run the sql query from outside - i.e. use your sql interface to do it and see what it returns. If it returns 1 then either might need to tweak the query or the "type" is not set correctly for all your nodes. If it returns something else - then revisit your code to see why it does not match up..
Are they "published?"
If they aren't marked as published they won't get counted.
Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database
NancyDru
The nodes are all published
The nodes are all published.
Could you please explain what the %s means in the db_query function? I realise it is a "placeholder" for the $mytype variable declared earlier in the script. At other posts I have seen %d used. I have tried changing to %d, but it still returns value of 1.
Please excuse me if my question is naive - I have very little knowledge of programming in general.
Thanks in advance.
Standard php
Those are standard php formats. "%d" means a decimal number; "%s" means a string.
Boy do I feel really blonde... Try this instead. Of course it would say 1 the way it was coded before -- a count query like that will always return one row - it's supposed to. We want the value, not the count of rows...
Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database
NancyDru
That works great! Is there a
That works great! Is there a way to do more than one content type? (not all)
Yes
Assuming that $mytype is an array of the types you want, change:
to:
Note: there are mixed single and double quotes there. Also, this is not the most secure code, so make sure you trust the source.
They will all be counted together.
Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database
NancyDru
I tried putting that in a
I tried putting that in a block with the PHP code input filter, but it doesn't show up. I copied and pasted the changed code, made sure to include the php tags around it, and placed the block to the right. I changed the content type names to match my own, but I'm not sure what I'm doing wrong. :(
That was a change
That was a change to the earlier post, so you still need the "echo" statement. This was intended as a Views header, but it should work in a block, as long as you also have the "echo" or some other means of actually outputting something to show.
Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database
NancyDru
I included this in my
I included this in my block:
Should this work? I don't get any php errors, it just doesn't show up.
Thanks nancy for taking the time to help me!
Sorry
I should always test before posting... This works on my site.
BTW, there was a PHP error as well as a SQL forming error.
Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database
NancyDru
Nancy, you're a sweetheart!
Nancy,
you're a sweetheart! thank you. I got some learning to do!
I tried putting that into a
I tried putting that into a footer, but it does not give me an option for input type of PHP. Also, I am using Drupal 6 (if that makes a difference) with the latest Views.
This is what I am entering...
Any thoughts?
Try
Try putting it in a block in the Content region - most themes put that at the bottom. Some themes have a footer region.
NancyDru
NancyDru
Worked perfectly.
Worked perfectly. Thanks...
Sinan
Thank you
Thank you for that!
It was exactly what I needed :)
Format the number
This snippet works perfectly for me and I have it in a block with several content types. However, I have thousands and the number is not formatted with a comma in the thousandths place. Does anyone know how to do this?
Try this
Note that number_format can handle other than English formatting; see http://us.php.net/manual/en/function.number-format.php
NancyDru
I ended up doing this and it
I ended up doing this and it is working great.
I figured it out before you reply - But thank you anyway.
Solution for D7?
How do you make a block in D7 that shows the number of nodes?
is there a way to put the
is there a way to put the number at the front of the article's title.thank you.
Thank you very much
Thank you very much for taking the time to help with this! Now it works perfectly.
Worst case scenario... the
Worst case scenario... the easy way would be to use "Views" and for the Argument type use "Summary".
This would be better for you because you could easily change filters such as published, content type, etc.etc.
Go to Views-->>Add-->> Arguments->> node type->> Summary view
I'll try that as well
I wasn't aware of that functionality under Argument in Views. Thanks for leading in that direction CChandler.
Thanks
That tip helped me out on another application.
Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database
NancyDru
You're welcome Nancy!
You're welcome Nancy!
Node count display for views
I'm stuck at a similar place. I've created a view which displays nodes (list mode) and fields associated with them. It also links to the taxonomy terms and nodes under it. I want to display the total number of nodes categorized under the particular taxonomy term, under each node listing in views but can't figure out how to. Could you guide me please. Thanks.
Coding
Can you please help me, where exactly should i put those code here?
I'd like to see the number of nodes in my node view of each content type also.
Thanks in advance.
Well...
The specific snippet given here goes in the header section of your View.
If you're looking for a count of all content types, there are other ways to do that.
Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database
NancyDru
Do you mean the header
Do you mean the header section of node.module or page.tpl.php?
I want to count only for one content type.
In the View
If you look in the Page section, you'll see several collapsed fieldsets, one is for header, one for footer, one for empty... Open the header and put the code there, and select "php" under the input format.
Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database
NancyDru
Where is that Page
Where is that Page section?
Can you tell me exactly the file name that i need to update?
E.g node.module or page.module or ....
View edit mode
The page section in View edit mode.
I got it. Thanks much.
I got it.
Thanks much.
Help count for CCK fieldtype
I have created a CCK field called "field_job_specialization" as integer. How is it possible for me to list / count and display it in a block like below:
I want to display the all the specialization available and a count of total number of jobs.
Job Specialization:
- Internet (118)
- Manufacturing (98)
- Pharmacy (21)
How can I achieve that? I think it needs a few database to be joining, but I'm no good at it... Can somebody help me?
Hmm...
Well the first thing I would do is to go back and ask myself, why is this a numeric field rather than a vocabulary?
Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database
NancyDru
CCK or Taxonomy... They Drive Me Crazy
Hi Nancy,
I'm not pretty sure myself either... CCK or Taxonomy? I would appreciate your opinion. You see, I'm trying to create a basic classified site that users can post in: For Sale, Autos, Jobs and Properties.
And, my questions above is generally to get it displayed on the left section (block). Initially, I set up those categories of the 4 areas mentioned as Taxonomy -> vocabulary. Then, I wanted to add some other text fields, for user input like:
Jobs
- Jobs category (Taxonomy -> vocab, where the dropdown has Business, Engineering, IT, Healthcare)
- Years of Experience (a CCK integer field)
- Salary (cck integer field)
- and other cck text fields.
Autos
- Autos category (Taxonomy -> vocab, where the dropdown has car makersFord, Toyota, Citroen, BMW)
- Car Year (a cck integer field)
- and other cck text fields
So, I would create a page called Jobs and Autos using the Views module. But, after creating it I see there's some limitation on what can be displayed. So, I was thinking if I should use CCK (text -> select list) to create and replace my Category / Taxonomy instead..
This CCK or Taxonomy problem on which path to take is really driving me crazy... as no other posts I found on those topics really give an actual answer. It's more like learning through experiences...
Lately there has been a
Lately there has been a great deal of conversation regarding using taxonomy and CCK Select List (acting as taxonomy) . See the newest Lullabot.com Podcast for 90 minutes of conversation regarding it.
You could always make years of experience a "FreeTagging" Vocabulary. I've read a few posts regarding Drupal's taxonomy system (in particular the taxonomy_term_count function) that are causing huge amounts of queries.
This link makes me shutter. http://2bits.com/articles/bottleneck-replacing-taxonomy-term-count-nodes...
Hmm...
That article is interesting, especially with the number of times I use the taxonomy_term_count_nodes function. However, I don't know how applicable that is here.
Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database
NancyDru
I've had similar quandaries
I've had similar quandaries in some of my sites. I've done things both ways and I still am not sure which is better.
Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database
NancyDru
Interesting to know...
Interesting to know... Thanks for sharing. Anyway I think I will proceed with Category (taxonomy module) for now... And see how it goes.
Maybe, once I'm done. I will repost it by sharing the use cases on dealing with the subject. Gosh.. CCK and Taxonomy, powerful yet confusing.
Cheers!
yeeloon
Here is a 90 minute Podcast
Here is a 90 minute Podcast on the Subject. http://www.lullabot.com/audiocast/podcast-48-taxonomy-taxonomy-taxonomy
Show node count of content "type A" contained in "type B"
Hello
I have a content type "Food Menu" and another content type called "Recipe". A user can add a number of recipes (node reference) in a Food Menu.
I want to be able to display the total number of recipes in the Menu teaser. Can you tell me how I can achieve this.
My site: www.thatscookedby.com
Regards
Vishal
Hello any suggestions please
Hello could any one suggest on how to do this, from my previous post :
Show node count of content "type A" contained in "type B"
I want to be able to display the number of "Recipes" a content type in the "Food Menu" Content type teaser
My site: www.thatscookedby.com
Regards
Vishal
For Drupal 7 node counting
See here for PHP snippets for Drupal 7 counts of nodes by node type:
http://drupal.stackexchange.com/questions/15871/count-of-nodes-by-type
For Drupal 7
Thanks to all this works to me!
Output:3
Thanks
Thanks
This point helped me a lot