Hey everyone,

I'm planning on making a taxonomy that will simply have the terms "yes" and "no". The taxonomy will relate to if a Rosary is given away or left at a given geocache visited by one of my users.

Ok, now to the real question. Is there a way to obtain a count of how many nodes are in the "yes" term and then make a block using that count?

Basically, is it possible to make a block that says "X Rosaries Given Away" X being the amount in the yes term of the Rosary taxonomy.

Thanks

God Bless,
Chris

Comments

nancydru’s picture

<?php  
    $tid = 8;              /* <---- change this to the correct taxo term number */
    $count = db_result(db_query("SELECT COUNT(nid) FROM {term_node} WHERE tid = " . $tid));
    print $count . " Yes votes";
?>

Nancy W.
now running 5 sites on Drupal so far
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in Your Database

jesusgeek83’s picture

Thanks, will give it a go!

jesusgeek83’s picture

Looks like that will work. Is there a way I can do the same thing with amount of terms in a given taxonomy and for the amount of users on the site? I am now thinking that doing a statistics page might be a better move than putting the information on the front page.

I am assuming you could just change some of the code to do that but, I wouldn't know what to change...

nancydru’s picture

<?php
  $vid = 1;         /* <---- put correct vocabulary ID here */
  $terms = taxonomy_get_tree($vid);      /* need code from below to handle nesting */
  print "<ul>";
  foreach ( $terms as $term ) { 
      $count = db_result(db_query("SELECT COUNT(nid) FROM {term_node} WHERE tid = %d ", $term->tid));
      if ($count) {   /* don't show terms with 0 count */
         print "<li>".$term->name." (".$count.")</li>";
       }
   } /* end foreach */
  print "</ul>";
?>

Nancy W.
now running 5 sites on Drupal so far
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in Your Database

jesusgeek83’s picture

Pretty neat! Is there a way to make it so that you just get a number of terms in the taxonomy, without listing them? Also, I seem to be getting a conflict with one of my modules. Could there be a variable being used with the same name in the module? In which case, can I change the variable names in the PHP code?

Here is the error I am getting:
warning: Invalid argument supplied for foreach() in c:\hosting\webhost4life\member\ctlw83\modules\profile\geocaching\geocaching.module on line 108.

Thanks again for all of your help!

God Bless,
Chris

nancydru’s picture

Good thing you reminded me, because I'm so tired I almost forgot about this function:
taxonomy_term_count_nodes(nnn) where nnn is the term number.

As long as the variable is not used in the same function, it should be okay. But the code I gave you would be better in a page rather than a module.

Nancy W.
now running 5 sites on Drupal so far
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in Your Database

Vic96’s picture

Is there any way to show this value in views?

bjacob’s picture

I think you should use the function taxonomy_term_count_nodes(). I didn't know that such a API function exists. So I took your initial function and realized that hidden nodes are also selected because the query doesn't take care about status = 0. So I came up with the following function:

function custom_kit_get_taxonomy() {
  $vid = 10;
  $terms = taxonomy_get_tree($vid);
  foreach ($terms as $term) {
    $term_count = db_result(db_query("SELECT DISTINCT COUNT({term_node}.nid) FROM {term_node} LEFT JOIN {node} ON {term_node}.nid = {node}.nid WHERE {term_node}.tid = %d AND {node}.status = %d", $term->tid, 1));
    if ($term_count > 0) {
      $term_path = drupal_get_path_alias('taxonomy/term/'.$term->tid);
      $output .= '<li>'.l($term->name.' ('.$term_count.')', $term_path, array('title' => $term->name.'('.$term_count.')')).'</li>';
    }
    global $user;
    if ($user->uid == 1) {
      $test = taxonomy_term_count_nodes($term->tid, 'startup');
      print $test;
    }
  }

  return $output;
}

Then I found taxonomy_term_count_nodes() which makes it easier - the whole query can be replaced. So I think one should use taxonomy_term_count_nodes(). What do you think?

nancydru’s picture

Yes, you can use taxonomy_term_count_nodes, but be aware that it is known for being less efficient. BTW, there is nothing wrong with including "status=1" directly in the query - it need not be a substituted parameter.

mauro72’s picture

This little modification is for to create a link to the term

<?php
  $vid = 2;         /* <---- put correct vocabulary ID here */
  $terms = taxonomy_get_tree($vid);      /* need code from below to handle nesting */
  print "<ul>";
  foreach ( $terms as $term ) {
      $count = db_result(db_query("SELECT DISTINCT COUNT(nid) FROM {term_node} WHERE tid = %d ", $term->tid));
      if ($count) {   /* don't show terms with 0 count */
         print "<li>".l($term->name .' ('. $count .')', "taxonomy/term/$term->tid") ."</li>";//this creates a link to the term
       }
   } /* end foreach */
  print "</ul>";
?>

robsteranium’s picture

Can the above code be modified to provide a pivot table to analyse term counts? For example count of terms by immediate book parent/ root parent.

Perhaps a view might be more appropriate?

Vic96’s picture

Is there any way to store this value in a CCK Computed Field? Thanks.

crbassett’s picture

Hi, I was wondering if its possible to show a count of how many *terms* there are in a certain vocabulary.

For example, I've used the code found elsewhere to say, "There are currently XXXX number of nodes in our database." I'd like something to say, "There are currently XXXX number of nodes filed under XXX subjects in our database."

I don't know PHP all that much, so I need some help, if anyone is willing to offer it.

Thanks.

archard’s picture

Term Node Count keeps track of node counts for all terms and gives that data to Views, which can be used in a block.

ibexy’s picture

bookmark