On Thu, 26 Jul 2001 10:27, CGI GUY wrote:
> What's the method for populating any number of html
> form <option>...</option> tags with query results?
> I've seen lots of php-embedded examples for CHECKBOX,
> RADIO and even SELECT, but I can't seem to figure out
> how to create a simple drop-down menu. HELP!!!

You only need to loop through the query result in the usual fashion and 
echo the results within option tags. Here's a function I wrote to to 
useful things with SELECT:

<?php
/* formDropDown - create an HTML <SELECT>
 * vars: $name - the form variable NAME
 *       $value - the SELECTED option which may be an array for multiple 
selected items
 *       $labels - assoc. array, list of values=>labels
 *               $multiple - if non-empty, select multiple
 *                 $size - number of rows to show if multiple
 * returns: string, HTML (i.e. for use in echo or print statement) */
function formDropDown($name,$value,$labels,$multiple = '',$size ='') {
  $html = '<SELECT NAME="' .$name . '"';
        if(!empty($multiple)) {
                $html .= ' MULTIPLE';
        }
        if(!empty($size)) {
                $html .= ' SIZE=' . $size;
        }
        
  $html .= ">\n";
  $key = key($labels);
  while($key != "") {
        /* Check if the current value is in the $value variable */
        if(is_array($value)) {
          if(in_array($key,$value)) {
        $selected = ' SELECTED';
      } else {
        $selected = '';
      }
        } else {
      if ($key == $value) {
        $selected = ' SELECTED';
      } else {
        $selected = '';
      }
        }
    $html .= "<OPTION VALUE=\"$key\"$selected>$labels[$key]\n";
    next($labels);
    $key = key($labels);
  }
  $html .= "</SELECT>\n";
  return $html;
}
?>
4ΘΡ4

-- 
David Robley      Techno-JoaT, Web Maintainer, Mail List Admin, etc
CENTRE FOR INJURY STUDIES      Flinders University, SOUTH AUSTRALIA  

   Why build a wall round a cemetery when no-one wants to get in?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to