Hi,

I have added the following code to my block output to display the taxonomy terms and the number of the times these terms appear on the nodes on the website:


<?php

$vid = 3;
$num_term = 20;

$query = "SELECT tid, name from {term_data} WHERE vid=$vid ORDER BY name ASC LIMIT $num_term";
$result = db_query($query);

     while ($term = db_fetch_object($result)) {
         $count = db_result(db_query("SELECT COUNT(nid) FROM {term_node} WHERE tid = %d", $term->tid));
         $items[]= l($term->name, "taxonomy/term/$term->tid") . '(' . $count . ')';
     }

if(count($items)) {
  return theme('item_list',$items, NULL, 'ul');
}

?>

It works fine, but it returns the output in slightly different format from what I want:

a) I want $items to be sorted by $count. I tried to use ksort, but didn't figure out how to do it.
b) I want the items to be displayed on the same line, not on the new line each time separated by double space.

Any hints would be very useful.

Thanks!

Comments

nevets’s picture

How about like this

<?php

$vid = 3;
$num_term = 20;

$query = "SELECT td.tid, name, COUNT(nid) as count from term_data td JOIN term_node tn USING(tid) WHERE td.vid=%d GROUP BY nid ORDER BY count DESC, name ASC LIMIT %d";
$result = db_query($query, $vid, $num_term);

     while ($term = db_fetch_object($result)) {
         $items[]= l($term->name, "taxonomy/term/$term->tid") . '(' . $term->count . ')';
     }

if(count($items)) {
  return implode(', ', $items);
}

?>
nevets’s picture

How about like this

<?php

$vid = 3;
$num_term = 20;

$query = "SELECT td.tid, name, COUNT(nid) as count from term_data td JOIN term_node tn USING(tid) WHERE td.vid=%d GROUP BY nid ORDER BY count DESC, name ASC LIMIT %d";
$result = db_query($query, $vid, $num_term);

     while ($term = db_fetch_object($result)) {
         $items[]= l($term->name, "taxonomy/term/$term->tid") . '(' . $term->count . ')';
     }

if(count($items)) {
  return implode(', ', $items);
}

?>
dmitry70’s picture

It didn't work quite well, as currently it shows terms in the following format: term1(2), term1(2), term1(2), term2(1), term1(1), term1(1) and about 14 term1(1) after that...

nevets’s picture

Ok, thats what I thought

b) I want the items to be displayed on the same line, not on the new line each time separated by double space.

meant. What do you want the output to look like?

dmitry70’s picture

Yes, but it doesn't count a) correctly. Because it has to count appearances of the terms on the nodes and currently it counts only one term - term1 many times. It should say: term1(22), term2(14), term3(12), etc. But instead it displays only term1 links as in my previous example. The output format is good, just the count is wrong.

nevets’s picture

Opps, GROUP BY nid should be GROUP BY td.tid

dmitry70’s picture

Thank you very much!

Do you know, what do I do to restrict the number of the terms to be displayed at the output, say to display only 7 first ones?

nevets’s picture

Change $num_term.

dmitry70’s picture

Thanks!