I need to do two things in my node template, but lack the PHP knowledge. This is what I need the code to do:

1) check through each term in a given vocabulary and if it has been selected, display it. Separate multiple terms with a comma and a space, but NOT put a comma after the final term selected. For instance in a sample node, in the vocabulary "colours" the terms red, green and blue are selected. The output should be:
Colours: red, green, blue

2) in one vocabulary I have several terms and I need to display a simple message if certain terms have been chosen. If none of the relevant terms are chosen, display no message.

Massively grateful for any help or pointers to where the answers lie :)

Comments

awebmanager’s picture

Anyone? *pretty please* :)

mean0dspt’s picture

start with snippets over here and below, modify to your needs
http://drupal.org/node/113651#comment-841679

awebmanager’s picture

Many thanks for that! I found some code and adapted it slightly:

<?php 
foreach ((array)$taxonomy as $item) { 
if ($item->vid == 2) {
print $item->name ;
print ", " ;
}
} 
?>

All I need now is some code which removes the comma after the final term is printed - I guess this could be achieved with a count of the total terms (in this vocabulary) in the node, and then an if statement saying "if this is the last term then don't print a comma, otherwise print a comma". Or maybe an if statement checking each term and saying "if there are more terms to show, print a comma, otherwise print nothing". I wouldn't know how to go about that though, any suggestions?