Could someone give me a hand? I am trying to run a query to display ONLY the taxonomy term parents with active links to a page showing all the child nodes within the parent node.

This is what I've got currently but I have 2 issues. 1) it is showing all the terms (parents and children) and 2) I need each link to be dynamic or utilize the call back function.

function companysearch_industry () {
 $vocabulary_id = 2;
 $result = db_query("SELECT a.tid, a.name FROM {term_data} a LEFT JOIN {term_hierarchy} b ON a.tid = b.parent 
WHERE a.vid = $vocabulary_id GROUP BY a.tid, a.name ORDER BY a.name");
  $items = array();
  while ($category = db_fetch_object($result)) {
    $items[] = l($category->name, 'industry');
  }
print theme('item_list', $items);
}

Comments

bfbryan’s picture

I don't want to get the parents of a specific tid rather a specific vid. Is that possible to do? Any help on how I make them into dynamic links to show the children?

dan33’s picture

Vocabularies can't have parents, so I'm a little confused. However, you can use this function to retrieve the terms for a specific vocabulary:

http://api.drupal.org/api/5/function/taxonomy_get_tree

It might be best if you browsed the taxonomy functions. Perhaps you'll find what you're looking for:

http://api.drupal.org/api/5/file/modules/taxonomy/taxonomy.module

mauro72’s picture

May be this is could help you.

<?php 
//in this case we use vocabulary 4, you must change it to the one you need.
$resultados = db_query ("SELECT DISTINCT t2.tid, t2.name
FROM `term_data` AS t2
INNER JOIN `term_hierarchy` ON parent = t2.tid
INNER JOIN `term_data` AS t1 ON term_hierarchy.tid = t1.tid
WHERE t1.vid = 4 ORDER BY t2.name ASC ");
while ($cats = db_fetch_object($resultados)) {
  $terms = taxonomy_get_tree( 4, $cats->tid);      /* need code from below to handle nesting */
print l($cats->name, "taxonomy/term/$cats->tid");
  print "<ul>";
  foreach ( $terms as $term ) {
      $count = db_result(db_query("SELECT DISTINCT COUNT(nid) FROM {term_node} WHERE tid = %d ", $term->tid));
      if ($count) {   /* don't show terms with 0 count */
         print "<li>".l($term->name .' ('. $count .')', "taxonomy/term/$term->tid") ."</li>";
       }
   } /* end foreach */
  print "</ul>";
}
?>
armand0’s picture

Only Parents, if the father is defined: only children of that parent. I believe that this is it that your you need.
This case is to be used with arguments in the URL, like: " /case/%/list "
But you can to use without arguments without changing nothing.
The only thing to modify is the the first line, the vid of the vocabulary to use.

Luck

<?php
// vocabulary to use
  $vid = 298;
// url parent term if exist
  $parent = arg(1);
// internal use
  $depth = -1;
// how depth search, 1parents, 2children, etc.
  $max_depth = 1;
// if no url arg, then set to cero.
  if (!is_numeric ($parent)) {
         $parent=0;
      }
// declare items array
  $items = array();
// get terms. If exist an URL parent get the children, if not get only the parents.
  $terms = taxonomy_get_tree($vid, $parent, $depth, $max_depth);
// use the terms, in this case show a list.
  foreach ( $terms as $term ) {
      $items[] = l($term->name, "taxonomy/term/$term->tid") ;
  }
  if ( count($items) ) {  print theme('item_list', $items);}
?>

Or drop down list:

<?php
$output = drupal_get_form('Paises_dropdown_form', $form);
return $output;

function Paises_dropdown_form() {
// vocabulary to use
  $vid = 298;
// url parent term if exist
  $parent = arg(1);
// internal use
  $depth = -1;
// how depth search, 1parents, 2children, etc.
  $max_depth = 1;
// declare items array
  $items = array();
// set form name
  $formname = 'Estado';
// if no url arg, then set to cero.
  if (!is_numeric ($parent)) {
         $parent=0;
         $formname = 'País';
      }
// get terms. If exist an URL parent get the children, if not get only the parents.
  $terms = taxonomy_get_tree($vid, $parent, $depth, $max_depth);
// use the terms, in this case show a list.
    $options[] = t('Seleccionar ' . $formname);
    //Populate array with url / name
        foreach ( $terms as $term ) {
            $options['/ubicacion/'.$term->tid.'/pruebada'] = $term->name;
        }
    //Build dropdown select
    //If we try to build OnChange directly it gets mangled, so put in array to confuse the forms api
    $form['category'] = array(
      '#type' => 'select',
      '#name' => $formname,
      '#id' => $formname,
      '#title' => '',
      '#default_value' => '',
      '#options' => $options,
      '#description' => '',
      '#multiple' => $multiple = FALSE,
      '#required' => $required = FALSE,
      '#attributes' => array('onChange' => 

"top.location.href=document.getElementById('$formname').options[document.getElementById('$formname').selectedIndex]

.value"),
    );
        return $form;
}
?>

bowwowadmin’s picture

How would I use this with this thread?
http://drupal.org/node/133223

I am trying and learning... :)
Eric

http://bowwowimport.com/exhaust

may the sun be on your back and Drupal in your face.

lonehorseend’s picture

It would be great to do something like this:

Node Title
Category name: parent-term>term, parent-term>term
Node Body

Skipping the current term if possible, so it only shows the other terms that the node maybe under.

lonehorseend’s picture

This is what I ended up doing after tons of research: http://drupal.org/node/235818#comment-865908

summit’s picture

Needing sister-terms solution through parent..
Subscribing, greetings, Martijn

Ankit22’s picture

Is thr a way for printing child terms only ?

Or skipping the first term will also do in my case

elangoa’s picture

Please use the below function it will return result array. You can change the $depth accordingly.

taxonomy_get_tree($vid, $parent = 0, $depth = 1);

print_r(taxonomy_get_tree($vid, $parent = 0, $depth = 1));

This will print the result array.