Terms in tree view - wrong order? I wanted to list the terms that a node has in a tree view. I've gotten that far that the list, sorted by the parent, is done, but the terms is in wrong order. Some terms becomes childterms to the wrong parent.

How do I fix that?

function my_module_cmp($a, $b) {

$a = (array) $a;

$b = (array) $b;

return strcmp($a['name'], $b['name']);

}
// A block with the current nodes linked categories in a tree view

function myfunction_category_block_contents($delta) {

// get nid from URL

$nid = arg(1);

$node = node_load($nid);

// get the terms using that nid

$terms = taxonomy_node_get_terms($node);

usort($terms, "hr_torget_cmp");



// create the head

$html = '&lt;div class="category-title"&gt;Kategorier&lt;/div&gt;';

$html .= '&lt;ul&gt;';



$i = 0;

foreach ($terms as $term){ 

    // the tid

    $tid = $term-&gt;tid;

    // the name

    $name = $term-&gt;name;

// DEBUG    $html .= $tid.'&lt;br&gt;';

    // get terms

    $parents = taxonomy_get_tree(10,$tid,NULL);

    // if the term is a parent
    if ($parents) {
        $html .= '&lt;li class="parent"&gt;&lt;a href="'.base_path().'kategori/'.$name.'"&gt;'.$name.'&lt;/a&gt;&lt;/li&gt;';
    }
    // if it isnt
    else {
        $html .= '&lt;ul&gt;';
        $html .= '&lt;li class="child"&gt;&lt;a href="'.base_path().'kategori/'.$name.'"&gt;'.$name.'&lt;/a&gt;&lt;/li&gt;';
        $html .= '&lt;/ul&gt;';
    }



$i++;

}



$html .= '&lt;/ul&gt;';



$html .= '&lt;img src="'.base_path().'sites/all/themes/hrtorget/images/verksamhet-bottom.png" alt="Bakgrundsbotten"&gt;';



return $html;

}
If I only get your goal correctly, I would suggest: for each node - get attached terms - for each term - get taxonomy_get_parents_all - reverse the array to have parent-to-child chain - form a list into a tree

Forming a tree could be yet another function with logic allowing to merge terms that share branches.