Posted by dmitry70 on July 4, 2009 at 10:00pm
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
How about like this
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);
}
?>
How about like this <?php$vid
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);
}
?>
Thanks for your response!
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...
Ok, thats what I thought b)
Ok, thats what I thought
meant. What do you want the output to look like?
Yes, but it doesn't count a)
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.
Opps, GROUP BY nid should be
Opps,
GROUP BY nidshould beGROUP BY td.tidYes, it works!
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?
Change $num_term.
Change $num_term.
Thanks!
Thanks!