Select list with taxonomies and node count

« Back :: Tagged with Drupal :: Drupal 6.x :: PHP :: Snippets ::

An example of a form with a select list of nodes with node count. This one also has the onchange attribute so it redirects the user on select. You do need to set up the Views view correctly for it to list by term id. 

<form id="form-taxonomy-dropdown" method="post" action="/dir">
  <?php
    // get terms from vocab 1
    $terms = taxonomy_get_tree(1);
    $node_type = 'story';
    
    $output = '<option value="">' . t('All Values') . '</option>';
    foreach ($terms as $t) {
      $count = taxonomy_term_count_nodes($t->tid, $node_type);
      if ($count != 0) {
        if (arg(1) == $t->tid) {
          $selected = 'selected="selected"';
        }
        else {
          $selected = '';
        }
        // only print value if there is a node connected
        $output .= '<option value="' . $t->tid . '" ' . $selected . '>' . $t->name . ' (' . $count . ')</option>';
      } // if count
    } // foreach
    if ($output) {
      print '<select id="select-taxonomy-dropdown" onchange="top.location.href=\'/' . arg(0) .'/\'+document.getElementById(\'select-taxonomy-dropdown\').options[document.getElementById(\'select-taxonomy-dropdown\').selectedIndex].value">' . $output . '</select>';
    }
  ?>
</form>

 

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <blockquote> <blockcode> <pre> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

Copyright © 2010 deuxcode.com
Powered by Drupal. Valid XHTML 1.0 Strict and CSS 2.1.