Hi,
I've found this - http://drupal.org/node/38768
Is it possible to get terms from two volabularies with one query to DB? (eg vocabulaires with id 1 and id 2)

Comments

nevets’s picture

If you just want a subset of the terms for a node for selected vocabularies you could do this

<?php
  // Change the list for t.vid IN () to set the vocabularies of interest
  $result = db_query("SELECT t.tid, t.name FROM {term_data} t, {term_node} r WHERE r.tid = t.tid AND r.nid = %d AND t.vid IN (1,2) ORDER BY weight, name", $node->nid);
  while ($term = db_fetch_object($result)) {
    $ntags[] = l($term->name, 'taxonomy/term/' . $term->tid);
  }
  if ($ntags) {
    print t("tags: ") . implode(', ', $ntags);
  }
?>

With Drupal 5 since you already have a node you can change this to

<?php
foreach ( $node->taxonomy as $term ) {
  switch ( $term->vid ) {
  case 1:  // Each case represents one vocabulary term you are interested.
  case 2:  // Modify as needed
     $ntags[] = l($term->name, 'taxonomy/term/' . $term->tid);
     break;
  }
}
  if ($ntags) {
    print t("tags: ") . implode(', ', $ntags);
  }
?>
which gets rid of the need for the database call.
fisty’s picture

WOW! I'm really shocked (in positive way) with speed and how good this answer is. It's exacly what I want! :) Big thanks nevets!
Now I can share code with You.
It replaces print $terms
with

<?php
if (!$page) {
foreach ( $node->taxonomy as $term ) {
  switch ( $term->vid ) {
  case 1:
     $ntags[] = l($term->name, 'taxonomy/term/' . $term->tid);
     break;
  }
}
  if ($ntags) {
    print t("Category: ") . implode(', ', $ntags);
  }
}
else {
foreach ( $node->taxonomy as $term ) {
  switch ( $term->vid ) {
  case 1:
  case 2:
     $ntags[] = l($term->name, 'taxonomy/term/' . $term->tid);
     break;
  }
}
  if ($ntags) {
print t("Category: ") . $ntags[0];
unset($ntags[0]);
print t("<br />Tags: ") . implode(', ', $ntags);
  }
}
?>

What I want to do is, on home page (and any other diffrent that node), show only "main" category witch is always one (single?) in my case, and when somebody enter a story show additionaly tags of this entry.
If there is a better way to do this I will hear it with pleasure.

(sorry form my english, unfortunatly I'm far away form native speaker ;] )

nevets’s picture

While the else case make work for you it would be probably better to 'else' code something like to avoid the assumption;

else {
foreach ( $node->taxonomy as $term ) {
  switch ( $term->vid ) {
  case 1:  // First set of catagories go in $category
     $category[] = l($term->name, 'taxonomy/term/' . $term->tid);
     break;
  case 2:  // The second set in $ntags
     $ntags[] = l($term->name, 'taxonomy/term/' . $term->tid);
     break;
  }
}
  if ($category) {
    print t("Category: ") . mplode(', ', $category);
  }
  if ($ntags) {
   print t("<br />Tags: ") . implode(', ', $ntags);
  }
}

An alternative approach to the 'else' section would be to list terms by vocabulary name like this

else {
$vocabs = array();

foreach( (array)$node->taxonomy as $term ) {
  $vocab = taxonomy_get_vocabulary($term->vid);
  if ( !isset($vocabs[$vocab->name]) ) {
  	$vocabs[$vocab->name] = array();
  }
  $vocabs[$vocab->name][] = l($term->name, "taxonomy/term/$term->tid");
}

foreach ( $vocabs as $name => $links ) {
  print '<div>' . $name . ': ';
  print implode(', ', $links);
  print '</div>';
}
}
fisty’s picture

Once again thank You. Your help is invaluable to me. If we meet somewhere You got beer from me. ;-)
I've simplified the whole code:

<?php
foreach ( $node->taxonomy as $term ) {
 switch ( $term->vid ) {
  case 1:
   $category[] = l($term->name, 'taxonomy/term/' . $term->tid);
   break;
  case 2:
   if ($page) { $ntags[] = l($term->name, 'taxonomy/term/' . $term->tid); }
   break;
  }
}
if ($category) {
 print t("Category: ") . implode(', ', $category);
}
if ($ntags) {
 print t("<br />Tags: ") . implode(', ', $ntags);
}
?>

I don't know is it well coded, but in think it's the shortest and simpliest way.

summit’s picture

Subscribing, greetings, Martijn