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
Shouldn't $zaznam be $sql?
greetings,
Martijn
The function could create
The function could create problem if ($prev_depth - $current_depth) >= 2. This is a fix (i hope :))
<?phpfunction _get_taxonomy_tree2($vid) {
$pole = array();
$items = array();
$terms = taxonomy_get_tree($vid);
$output = '<li>' . taxonomy_vocabulary_load($vid)->name;
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)", $term->depth, $count, $term->tid
);
}
$depth = -1;
foreach ($pole as $list) {
if ($list[1] > $depth) $output .= "\n<ul>";
if ($list[1] < $depth) {
for ($i=$list[1]; $i<$depth; $i++)
$output .= "\n</li>\n</ul>\n</li>";
}
if ($list[1] == $depth) $output .= "</li>";
$output .= "\n<li>$list[0]";
if ($list[2] > 0) {
$output .= "\n<ul>";
$result = db_query("SELECT * FROM {term_node} WHERE tid=$list[3]");
$zaznam = db_fetch_array($result);
while ($zaznam) {
$node = db_result(db_query("SELECT title FROM {node} WHERE nid=$zaznam[nid]"));
$node_link = l($node, "node/$zaznam[nid]");
$output .= "\n<li>$node_link</li>";
}
$output .= "\n</ul>";
}
$depth = $list[1];
}
for ($i=0; $i<=$depth; $i++)
$output .= "\n</li>\n</ul>\n</li>";;
return $output;
}
?>
___________________
Katapekkia | Multiblog Sociale
Psicomante Blog
Psicomante's Themes for Drupal