Full Taxonomy tree with nodes

Last modified: September 20, 2007 - 07:35

This code outputs the full taxonomy tree, including nodes that belongs to terms.

<?php
$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") . " ($count)", $term->depth, $count, $term->tid)  ;
}
 
$depth =-1;
  foreach (
$pole as $list) {
    if (
$list[1] > $depth) echo "\n<ul>";
    if (
$list[1] < $depth) echo "\n</li>\n</ul>\n</li>";
    if (
$list[1] == $depth) echo "</li>";
   
$poc++;
    echo
"\n<li>$list[0]";

if (
$list[2]>0) {
      echo
"\n<ul>";
     
$result = db_query("SELECT * FROM {term_node} WHERE tid=$list[3]");
      while(
$zaznam = db_fetch_array($result)) {
       
$node = db_result(db_query("SELECT title FROM {node} WHERE nid=$zaznam[nid]"));
       
$node_link = l($node, "node/$zaznam[nid]");
        echo
"\n<li>$node_link</li>";
      }
      echo
"\n</ul>";
  }
   
$depth=$list[1];
}
echo
"</li>\n</ul>";
?>

I really need that full taxonomy tree with nodes, but I cannot found any solution, there are only plenty of taxonomy trees without nodes. It's my first piece of code that I wrote in PHP ever, so this code might need to be revised by more skilful people.

Shouldn't $zaznam be

Summit - September 10, 2008 - 13:03

Shouldn't $zaznam be $sql?

greetings,
Martijn

 
 

Drupal is a registered trademark of Dries Buytaert.