Add an taxonomy to a certain node type

This code adds a term for each new node of a certain node type based on the title.
It uses a hard coded Vocabulary name and search for its "vid". When the title on the node is changed it changes the terms name as well. When the node is deleted the term is deleted to.

<?php
/**
*  Makes a copy of CI name into taxonomy voc INDICATION
*  Makes it easier for user to register a new CI
*
**/
function biogaia_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {

  if(
$node->type == "clinical_indication"){
    switch (
$op) {
      case
'load':
      case
'view':
        break;
      case
'insert':
       
$indication = db_fetch_array(db_query('SELECT vid FROM {vocabulary} WHERE name LIKE "INDICATION"'));
       
$form_values = array("vid" => $indication['vid'],  "name" => $node->title);
       
taxonomy_save_term($form_values);
       
$term = db_fetch_array(db_query('SELECT tid FROM {term_data} WHERE name LIKE "%s" AND vid = %d', $node->title, $indication['vid']));
       
$node_taxonomy = array($indication['vid'] => array($term['tid'] => $term['tid']));
       
taxonomy_node_save($node, $node_taxonomy);

        break;
      case
'update':
       
$indication = db_fetch_array(db_query('SELECT vid FROM {vocabulary} WHERE name LIKE "INDICATION"'));
       
$term = db_fetch_array(db_query('SELECT * FROM {term_node} LEFT JOIN {term_data} USING(tid) WHERE nid = %d AND {term_data}.vid = %d', $node->nid, $indication['vid']));

        if(
$node->title != $term['name']) {
         
db_query('UPDATE {term_data} SET name="%s" WHERE tid = %d LIMIT 1', $node->title, $term['tid']);
        }

        break;
      case
'delete':
       
//delete term_data
       
$indication = db_fetch_array(db_query('SELECT vid FROM {vocabulary} WHERE name LIKE "INDICATION"'));
       
db_query('DELETE FROM {term_data} WHERE name LIKE "%s" AND vid = %d LIMIT 1', $node->title, $indication['vid']);
        break;
    }
  } 
/**/


}
?>

A save taxonomy term to node, free tagging script

<?php
/**
* Change "Parents" to your vocabulary
*/
function save_term($node, $taxonomy_term){
   
$indication = db_fetch_array(db_query('SELECT vid FROM {vocabulary} WHERE name LIKE "Parents"'));
   
$form_values = array("vid" => $indication['vid'],  "name" => $taxonomy_term);
   
taxonomy_save_term($form_values);


   
$term = db_fetch_array(db_query('SELECT tid FROM {term_data} WHERE name LIKE "%s" AND vid = %d', $taxonomy_term, $indication['vid']));
   
$node_taxonomy = array($indication['vid'] => array($term['tid'] => $term['tid']));
   
taxonomy_node_save($node, $node_taxonomy);

}
?>
Knowledge keywords: