Re: Re: Re: Re: Menus based on Taxonomy terms

pjriot's picture
Offline
Joined: 12/14/2008
Juice: 41
Re: Re: Re: Re: Menus based on Taxonomy terms

In case anyones interested I created a simple view called testview that took the Taxonomy: Term ID as an argument. This was for accessing a given term. The only solution I could find for generating the menu was to do it manually.

So in a nutshell, this code accepts a brand tid, searches for all products belonging to that brand (tid), finds out where the products sit in the catalog and generates a menu *specifically for that brand*.

I'm guessing there's a better way to do this but damned if I know...

function get_catalog_by_term($brand) {
  $terms = array(0 => $brand);
  $menu = array();
 
  foreach($terms as $term) {
  $result = db_query('SELECT t.nid FROM {term_node} t WHERE t.tid = %d', $term);
while ($node = db_fetch_array($result)) {
$nodes[] = $node['nid'];

$catresult = db_query('SELECT t.tid FROM {term_node} t WHERE t.nid = %d AND t.tid IN (SELECT t.tid FROM {term_node} t INNER JOIN {term_data} d on t.tid = d.tid WHERE d.vid = 1)', $node['nid']);
while ($cat = db_fetch_array($catresult)) {
$categories[] = $cat['tid'];
$parents = taxonomy_get_parents_all($cat['tid']);
$menu = parentArray($menu, $parents, $node['nid']);
}
}
  }
  return $menu;
}

function parentArray($menu, $parents, $nid) {
$parentArr = $menu;
$tmp = &$parentArr;
foreach(array_reverse($parents) as $parent) {
if(!isset($tmp[$parent->name . '-' . $parent->tid])) {
$tmp[$parent->name . '-' . $parent->tid] = array();
}
$tmp = &$tmp[$parent->name . '-' . $parent->tid];
}
return $parentArr;
}

function printArrayList($array, $brand) {
foreach($array as $key => $value) {
if(is_array($array[$key])) {
preg_match("/[0-9]+\z/", $key, $groups);
$tid = $groups[0];
$category = substr($key, 0, strlen($key) - (strlen($tid)+1));
print '<ul class="brandmenu"><li><a href="/testview/' . $tid . ',' . $brand . '">' . $category . '</a></li>';
printArrayList($array[$key], $brand);
print '</ul>';
}
}
}

from my page.tpl.php I call them like so:

$arr = get_catalog_by_term('115');
printArrayList($arr, '115');

Just looking at this now, I'm not sure why I have a foreach loop in the first function, I'm sure there was a good reason but can't remember atm...

Anyway,this might come in useful for someone.

Menus based on Taxonomy terms By: pjriot (7 replies) Wed, 02/04/2009 - 02:06