How to loop through a taxonomy vocabulary to find the top category in a nested tree? What I have so far is this

$term=therm_id_that_I_already_have;

$parent = db_result(db_query("SELECT h.parent FROM {term_data} d INNER JOIN {term_hierarchy} h ON d.tid = h.tid WHERE d.tid = $term"));
$term = $parent;


$th=taxonomy_get_term($term);


probably I need a while of something...
I would recommend using taxonomy_get_parents_all(). If you have a vocabylary that looks like:


Vocabulary

Child 1 (tid=1)

Child 1.1 (tid=3)

Child 2 (tid=2)



Then you can do:

&lt;?php
$parents = taxonomy_get_parents_all(3);
$top_category = array_pop($parents);


Will give you the term for tid 1.