taxonomy

How to get parent to taxonomy term

This is how you can get the parent term to a taxonomy term.

<?php

$tid
= 4;
parent = taxonomy_get_parents($tid);


echo
"<pre>";
print_r($parent);
echo
"</pre>";


//Will print something like this
/*
Array
(
    [3] => stdClass Object
        (
            [tid] => 3
            [vid] => 1
            [name] => My parent term name
            [description] =>
            [weight] => 0
        )

)
*/

?>

Get term from modules vocabulary

<?php
function atrium_opportunity_get_term($value="New", $voc = "features_opportunity_state"){

 
$vid = db_result(db_query("SELECT vid FROM {vocabulary} WHERE module = '%s';", $voc));

 
$term = db_fetch_array(db_query('SELECT tid FROM {term_data} WHERE name LIKE "%s" AND vid = %d', $value, $vid));

  return array(
$vid => array($term['tid'] => $term['tid']));
}
?>

Drupal taxonomy term function


/**
* Get for l_keywords from vocabulary to
* dropbox in widget
*
*/
function _get_l_keywords() {

$vid = cdt_keywordlink_get_vocabulary();
$pole = array();
$items = array();
$terms = taxonomy_get_tree($vid);

if($result = db_query('SELECT * FROM {term_data} WHERE vid = %d', $vid)) {

while ($data = db_fetch_object($result)) {
$matches[$data->tid] = check_plain($data->name);
}
}

if (count($matches) > 0) {
return $matches;
}else{
drupal_set_message("There is no l_keywords registered in the cdt_keywordlink vocabu

How to add an Vocabulary in an install file

This is how you add an Vocabulary in an install file or in a module.

/**
* Install
*
**/
...
$vocab_count = db_query("SELECT COUNT(vid) AS vid_count FROM {vocabulary}
WHERE name = '%s'", 'A name');

$vocab_count = db_fetch_object($vocab_count);
$vocab_count = $vocab_count->vid_count;

if ($vocab_count == 0){
$vocab = array();

$vocab['name'] = 'A name';
$vocab['description'] = 'A description';
$vocab['help'] = 'Some help';
$vocab['multiple'] = 0;
$vocab['required'] = 1;
$vocab['module'] = 'zimplicit';
$vocab['no

Loop all terms in a vocabulary

function list_all_countries($vid) {
$vid = 1; // Set the vid to the vocabulary id of the vocabulary you wish to list the terms from
$pole = array();
$items = array();
$terms = taxonomy_get_tree($vid);

foreach ( $terms as $term ) {
$count = db_result(db_query("SELECT COUNT(nid) FROM {term_node} WHERE tid = %d", $term->tid));
$pole[]=Array (l($term->name, "taxonomy/term/$term->tid") .