What's the drupal way of producing this code (which does select box navigation)?


<select onchange="navigate()">
     <option value="url1">Place 1</option>
     <option value="url2">Place 2</option>
     <option value="url3">Place 3</option>
</select>

Assuming I have two parallel arrays with urls and labels.

Ordinarily, I would just iterate through the arrays and build my options list, but it seems like there must be a more drupal-like way of handling this.

Comments

mpare’s picture

I'm not completely understanding the question, but if you have two arrays one for keys and one for values us array_combine(). If this is a form your probably using form api. So you would feed your combined array as options to the form array. If your wanting your user to navigate the site using the select then you would use hook_submit find out what value was selected then use drupal_goto() depending on which option was selected. More information can on drupal's Form API can be found at http://api.drupal.org/api/file/forms_api_reference.html/5.

Cheers,
-mpare
--
Pare Technologies
Drupal Consulting, Themeing, and Module Development
806.781.8324 | 806.733.3025
www.paretech.com

Figure Something Out? Document Your Success!

eoneillPPH’s picture

Here's something I did that might help someone else. Using Drupal 5.x
I created a block to show a select box menu that immediately takes the user to the node/item selected. Next I want to figure out how to pop that block into a view that also needs to use it, but that's another issue. Here's my code in the Block Body set to Input Format of 'PHP code':

  $query = "SELECT DISTINCT nid, title FROM node 
WHERE `type`='[my type]' 
  AND status=1
  AND nid in (
    [big sub-select to get just certain values in a related CCK table]
    )
ORDER BY title
";
  $result = db_query($query);
  $options = ''; $count=0;
  while ($row = db_fetch_array($result)) {
   $options .= '<option value="/node/'.$row['nid'].'">'.$row['title'].'</option>';
   $count++;
  }
  $form = '<form action=""><select name="jumpto" id="jumpto" onchange="window.location.href=document.getElementById(\'jumpto\').options[document.getElementById(\'jumpto\').selectedIndex].value">'.$options.'</select></form>';
if ($count > 0) print $form;

(Make sure that javascript 'onchange' line doesn't have any spaces in it.)

Hope that helps someone...