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

nancydru’s picture

A Views header can contain php code (there is an input filter field set).

<?php
  $mytype = 'job';  //   <<---------- set your content type here.
  $results = db_query("SELECT COUNT(DISTINCT(nid)) FROM {node} WHERE type='%s' AND status=1", $mytype);
  $count = db_num_rows($results);
  echo "There are currently $count jobs in our database.";
?>

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

bestknight’s picture

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)

chadchandler’s picture

Post the code you put into the header here. Also make sure your input is set to PHP.

bestknight’s picture

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?

panis’s picture

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..

nancydru’s picture

bestknight’s picture

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.

nancydru’s picture

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...

<?php
  $mytype = 'job';  //   <<---------- set your content type here.
  $count = db_result(db_query("SELECT COUNT(DISTINCT(n.nid)) FROM {node} n WHERE n.type='%s' AND n.status=1", $mytype));
  echo "There are currently $count jobs in our database.";
?>

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

Fayna’s picture

That works great! Is there a way to do more than one content type? (not all)

nancydru’s picture

Assuming that $mytype is an array of the types you want, change:

  $mytype = 'job';
  $count = db_result(db_query("SELECT COUNT(DISTINCT(n.nid)) FROM {node} n WHERE n.type='%s' AND n.status=1", $mytype));

to:

  $mytype = array('job', 'story');
  $count = db_result(db_query("SELECT COUNT(DISTINCT(n.nid)) FROM {node} n WHERE n.type IN(%s) AND n.status=1", "'". implode("', '", $mytype) ."'");

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

Fayna’s picture

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. :(

nancydru’s picture

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

Fayna’s picture

I included this in my block:

<?php
$mytype = array('page', 'story');
  $count = db_result(db_query("SELECT COUNT(DISTINCT(n.nid)) FROM {node} n WHERE n.type IN(%s) AND n.status=1", "'". implode("', '", $mytype) ."'");
  echo "There are currently $count nodes in our database.";
?>

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!

nancydru’s picture

I should always test before posting... This works on my site.

<?php
  $mytype = array('page', 'story');
  $in = implode("', '", $mytype);
  $count = db_result(db_query("SELECT COUNT(DISTINCT(n.nid)) FROM {node} n WHERE n.type IN('$in') AND n.status=1"));
  echo "There are currently $count nodes in our database.";
?>

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

Fayna’s picture

Nancy,

you're a sweetheart! thank you. I got some learning to do!

shaneburger’s picture

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...

<?php
$mytype = 'workshop_application'; // <<---------- set your content type here.
$count = db_result(db_query("SELECT COUNT(DISTINCT(n.nid)) FROM {node} n WHERE n.type='%s' AND n.status=1", $mytype));
echo "There are currently $count applications for review.";
?>

Any thoughts?

nancydru’s picture

Try putting it in a block in the Content region - most themes put that at the bottom. Some themes have a footer region.

NancyDru

Sinan Erdem’s picture

Worked perfectly. Thanks...

Sinan

ntg’s picture

Thank you for that!
It was exactly what I needed :)

BlueBlossom’s picture

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?

nancydru’s picture

$mytype = 'workshop_application'; // <<----- set your content type here.
$count = db_result(db_query("SELECT COUNT(DISTINCT(n.nid)) FROM {node} n WHERE n.type='%s' AND n.status=1", $mytype));
echo t('There are currently !count applications for review.', array('!count' => number_format($count)));

Note that number_format can handle other than English formatting; see http://us.php.net/manual/en/function.number-format.php

BlueBlossom’s picture

I ended up doing this and it is working great.

$mytype = 'workshop_application';  //   <<---------- set your content type here.
$result = db_result(db_query("SELECT COUNT(DISTINCT(n.nid)) FROM {node} n WHERE n.type='%s' AND n.status=1", $mytype));
$count_total = number_format(($result)); // total number of nodes w/ formatting
echo "$count_total fabrics<br/> ";

I figured it out before you reply - But thank you anyway.

thomas1977’s picture

How do you make a block in D7 that shows the number of nodes?

runeveryday’s picture

is there a way to put the number at the front of the article's title.thank you.

bestknight’s picture

Thank you very much for taking the time to help with this! Now it works perfectly.

chadchandler’s picture

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

bestknight’s picture

I wasn't aware of that functionality under Argument in Views. Thanks for leading in that direction CChandler.

nancydru’s picture

chadchandler’s picture

You're welcome Nancy!

Vic96’s picture

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.

ljet’s picture

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.

nancydru’s picture

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

ljet’s picture

Do you mean the header section of node.module or page.tpl.php?
I want to count only for one content type.

nancydru’s picture

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

ljet’s picture

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 ....

bestknight’s picture

The page section in View edit mode.

ljet’s picture

I got it.
Thanks much.

yeeloon’s picture

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?

nancydru’s picture

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

yeeloon’s picture

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...

chadchandler’s picture

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...

nancydru’s picture

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’s picture

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

yeeloon’s picture

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

chadchandler’s picture

vishalchavda’s picture

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

vishalchavda’s picture

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

hongpong’s picture

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

tajdar’s picture

Thanks to all this works to me!

$query = new EntityFieldQuery();

$query->entityCondition('entity_type', 'node') // grab nodes
->entityCondition('bundle', 'page') // Enter your content type name e.g page,article etc..
->propertyCondition('status', 1) // filter by published
->count(); // count

$result = $query->execute();
print $result;


Output:3

blogers’s picture

Thanks

This point helped me a lot